简体   繁体   中英

Angular $http.json promise returning error, though I can see the response

I am somewhat new to working with http promise objects.

I am using Angular JS to return json from an API http://www.asterank.com/api .

I have an angular controller making the call like so:

  $http.jsonp('http://www.asterank.com/api/asterank?query={%22e%22:{%22$lt%22:0.1},%22i%22:{%22$lt%22:4},%22a%22:{%22$lt%22:1.5}}&limit=1&callback=JSON_CALLBACK').
success( function(data) {
  console.log('great success');
}).error( function(r,t,et){
  console.log(r);
  console.log(t);
  console.log(et);
});

When I check out Chrome's network monitor I see the response:

HTTP/1.1 200 OK
Date: Sun, 06 Oct 2013 19:06:26 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Server: gunicorn/18.0
Set-Cookie: session=eyJkaXNjb3Zlcl9maXJzdF90aW1lIjpmYWxzZX0.BTNGMg.eF89vHEeIpLH8sZiJOwCAJEjPhA; HttpOnly; Path=/
Content-Encoding: gzip

But I am seeing the error method fire, never the success :(

Is this simply because the server does not support JSONP? How do you access the data of these APIs if they don't support JSONP but they support JSON?

Found a nice solution:

Just in case anyone comes across this and like me am using EXPRESS, you create create a simple little API on your server using this:

https://npmjs.org/package/request

Here I don't need to spin up a whole proxy server, but you can request the JSON data from your server.

Only problem here is that site doesn't even declare JSONP support.

How do you access the data of these APIs if they don't support JSONP but they support JSON?

Write your proxy layer on backend.

Try like this:

var url = 'http://www.asterank.com/api/asterank?query={%22e%22:{%22$lt%22:0.1},%22i%22:{%22$lt%22:4},%22a%22:{%22$lt%22:1.5}}&limit=1&callback=json_callback';

$http({method: 'GET', url: url })
                .success(function(data,status,headers,config){
                    console.log(data);
                }).error(function(data,status,headers,config){
                    console.log('API CALL ERROR'+status);
                });
        };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM