node.js - Sending JSON Data from $http to Node JS Express restful service -


what best way send data angular js node js restful service uses express. angular js code follows:-

var mycontroller = function("mycontroller", $scope) { $http.get("/someurl", {key1:"value1", key2: "value2"}).then(function(response) { alert(response.data);}); 

on node js end :-

app.get("/someurl", function(req, res) {console.log(req.body);}); 

i can see console.log displaying {}. there way send json input well.

by way nodejs used express module, have used body-parser , set other things needed.

i using angularjs 1.4 , nodejs express 4.

you can use both $resource , $http. $resource can leverage consistency of rest apis, since use familiar structure build endpoints, there's little magic going on.

using $resource:

var dog = $resource('/dogs/:dogid', { dogid :'@id' });  var fido = new dog({ name: "fido" }); fido.$save();  // post /dogs/ data: { name: "fido" } 

with $http you're more explicit. can send data this:

$http({     method: "get",     url: "...",     params: {         key1: "value1",         key2: "value2"     } }); 

and this:

$http({     method: "post",     url: "...",     data: {         key1: "value1",         key2: "value2"     } }); 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -