How can i get json data from web service in Angularjs?

This is my web service, i send it 2 post vars from an app mobile to the request and response a json object.

var express = require('express');
var app = express();
var port = process.env.PORT || 8080;

var bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies

app.post('/coords', function(req, res) {

    var lat         = req.body.lat,
        lon         = req.body.lon;

    res.setHeader('Content-Type', 'application/json');
    res.send(JSON.stringify({"vehicle": [{ latitud: lat, longitud: lon }]}));

});


// start the server
app.listen(port);
console.log('Server start! at http://localhost:' + port);

Then i would like to obtain that json in my angularjs controller. I´m not sure if is with this form

var app = angular.module('myApp',[]);
app.controller('vehicleCtrl', function($scope, $http) {

    var url = "http://localhos:8080/coords";

    $http.jsonp(url)
        .success(function(data){
            console.log(data.vehicle);
        });

});

Answers


I've tried to create an example that has similar code. An aditional thing what I did to prevent XOrigin issues was to put the index.html file below a public directory wich is provided by the line app.use(express.static('public')); in server.js.

index.html:

<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="bower_components/angular/angular.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        var app = angular.module('myApp',[]);
        app.controller('demoCtrl', ['$scope', '$http', function($scope, $http) {
            $scope.restart =function(){
                console.log('Restart');
                var url = 'http://localhost:8080/coords';
                $http.post(url)
                        .success(function(data){
                            console.log(data.vehicle);
                        });
            }
        }]);

    </script>
</head>
<body>
<div  ng-controller='demoCtrl'>
    <h1>Test</h1>
    <p><button ng-click="restart()">Restart</button></p>
</div>
</body>
</html>

server.js:

var express = require('express');
var app = express();
var port = process.env.PORT || 8080;

var bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
app.use(express.static('public'));
app.post('/coords', function(req, res) {

    var lat         = req.body.lat,
        lon         = req.body.lon;

    res.setHeader('Content-Type', 'application/json');
    res.send(JSON.stringify({"vehicle": [{ latitud: "foo", longitud: "bar" }]}));

});


// start the server
app.listen(port);
console.log('Server start! at http://localhost:' + port);

Need Your Help

c++ compare function result

c++ gcc g++

I have the following code that is exhibiting some very strange behavior. does anyone have any idea why.