角美元http。json承诺返回错误,尽管我可以看到响应。

时间:2022-03-03 07:06:14

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.

我正在使用角度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? How do you access the data of these APIs if they don't support JSONP but they support JSON?

这仅仅是因为服务器不支持JSONP吗?如果这些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数据。

2 个解决方案

#1


1  

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.

在后端编写代理层。

#2


0  

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);
                });
        };

#1


1  

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.

在后端编写代理层。

#2


0  

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);
                });
        };