Jdizzle Jdizzle - 2 years ago 132
Node.js Question

NodeJs API with Angular does not return JSON result

UPDATE: I tried another API: http://www.omdbapi.com/?t=Sherlock

And the GET works. However, I'm still unsure why my node.js server API does not work with this method.

I am currently working on the backend of an app of which I created an API similar to the tutorial: https://codeforgeek.com/2015/03/restful-api-node-and-express-4/.

However, I cannot seem to get my ionic angular app to retrieve my JSON result. When I put my get command on Postman, I get :


My controller is:

.then(function(response) {
$scope.status = response.status;
$scope.data = response.data;
$log.log("res:"+ response);
}, function(response) {
$scope.data = response.data || "Request failed";
$scope.status = response.status;
$log.log("res:"+ response);

$log.log("status: "+$scope.status);
$log.log("data: "+$scope.data);

(my /fr references the JSON which has a GET method on my node server)

And always gives the result of "failed", "status: undefined" as well as "data: undefined". When I recurse this method, it gives me a "status:0".

I have also tried using $resource with no success.
Any guidance would be appreciated. My API does not have any auth and I don't think it has anything to do with CORS(unless you guys think it does).

this is the get method on my server.js:

//GET Floor
var query = "SELECT floor FROM stor1 WHERE id=0";

if(err) {
res.json({"Error" : true, "Message" : "Error executing MySQL query"});
} else {

Well, i added more logs to display results and now i get an [object Object] from response. The object is:
{"data":null,"status":0,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"url":"http://localhost:3000/api/fl","headers":{"Accept":"application/json, text/plain,*/*"}},"statusText":""}

Answer Source

Your postman seems to get the response for the GET request which means your server side code is fine with routing. But your Angular App is unable to get any response with out throwing any error in the server side. Which seems to be the case of CORS

var cors = require('cors');    

Note: Use this middleware before your first route.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download