0

Simple node HTTP server is hosting JSON data on localhost/6666.

Another HTTP server is setup to serve very basic angular.js application made of single index.html file and module which purpose is to fetch data:

app.controller('ctrl', function ($scope, $http) { $http({ method: 'GET', url: 'localhost:6666/' }).then(function (res) { $scope.dt = res.data }); }); 

Using developer tools in my browser I was able to read error log from the console, that looks like this:

Possibly unhandled rejection: {"data":null,"status":-1,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"localhost:6666/","headers":{"Accept":"application/json, text/plain, /"}},"statusText":"","xhrStatus":"error"}

Perhaps this is because angular is asking for resources that are cross-origin, or maybe it has something to do with the way that resources are served. When I search for localhost:6666 (where data is served), I see only JSON string that is served using the following code:

http.createServer(function(req, res) { res.writeHead(200, { "Content-Type": "application/json", 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'OPTIONS, POST, GET', 'Access-Control-Max-Age': 24000, }) res.end(JSON.stringify(data)) }).listen(6666) 

Where "data" is ordinary javascript object.

3
  • Shouldn't it be 'http://localhost:6666/'?CommentedMar 16, 2019 at 20:07
  • 1
    Yes Alon Eitan, thanks for pointing that out. I was unable to solve the error log myself. Now I'm able to see data displayed on my website :)
    – adict
    CommentedMar 16, 2019 at 20:11
  • 1
    Great! If the problem is now solved then I suggest that you delete this question because it is a very common error and there are probably lot's of duplicates about the exact same issue :)CommentedMar 16, 2019 at 20:13

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.