[英]Calling a PHP returning a JSON returns an error though I can see the response
[英]Angular $http.json promise returning error, though I can see the response
我對使用HTTP Promise對象有些陌生。
我正在使用Angular JS從API http://www.asterank.com/api返回json。
我有一個角度控制器,像這樣打電話:
$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);
});
當我檢出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
但是我看到錯誤方法失火了,從來沒有成功:(
這僅僅是因為服務器不支持JSONP嗎? 如果這些API不支持JSONP但支持JSON,如何訪問這些數據?
以防萬一有人遇到這個問題並且像我一樣使用EXPRESS,您可以使用以下方法在服務器上創建一個簡單的小API:
https://npmjs.org/package/request
在這里,我不需要啟動整個代理服務器,但是您可以從服務器請求JSON數據。
這里唯一的問題是該站點甚至沒有聲明JSONP支持。
如果這些API不支持JSONP但支持JSON,如何訪問這些數據?
將您的代理層寫在后端。
嘗試這樣:
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.