简体   繁体   English

角度$ http.json承诺会返回错误,尽管我可以看到响应

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

I am somewhat new to working with http promise objects. 我对使用HTTP Promise对象有些陌生。

I am using Angular JS to return json from an API http://www.asterank.com/api . 我正在使用Angular JS从API http://www.asterank.com/api返回json。

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: 当我检出Chrome的网络监视器时,会看到以下响应:

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? 这仅仅是因为服务器不支持JSONP吗? How do you access the data of these APIs if they don't support JSONP but they support JSON? 如果这些API不支持JSONP但支持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: 以防万一有人遇到这个问题并且像我一样使用EXPRESS,您可以使用以下方法在服务器上创建一个简单的小API:

https://npmjs.org/package/request 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. 在这里,我不需要启动整个代理服务器,但是您可以从服务器请求JSON数据。

Only problem here is that site doesn't even declare JSONP support. 这里唯一的问题是该站点甚至没有声明JSONP支持。

How do you access the data of these APIs if they don't support JSONP but they support JSON? 如果这些API不支持JSONP但支持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);
                });
        };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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