【问题标题】:AngularJS RESTAPI call throwing errorAngularJS REST API调用抛出错误
【发布时间】:2015-11-07 02:50:22
【问题描述】:

我正在学习 AngularJS。我试图使用标准的$http 服务进行 RESTapi 调用,但没有得到成功的响应。任何线索我在这里做错了什么?

我正在调用 URL(当我 ping 它时,URL 呈现正常):http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=json

我收到的错误是:

{"data":null,"status":0,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"url":"http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote","params":{"format":"json"},"headers":{"Accept":"application/json, text/plain, */*"}},"statusText":""}

我的 Angularjs 测试代码是:

angular.element(document).ready(function(){
angular.bootstrap(document.getElementById('app2'),['app2']);
});

angular.module('app2',[])
  .controller('testNamesCtrl',function($scope,$http){

        $http({method:'GET',url:'http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote',params:{format:"json"}})
        .then(
                function(response){
                    $scope.record = response.data;
                $scope.record1 = response;
            }, function(response){
                $scope.record1 = response;
            });
  });

【问题讨论】:

  • 你得到了什么 HTTP 状态码?检查浏览器控制台(网络选项卡),可能已经有一些线索了。
  • .jsonp 可能是 cmets 中建议的答案。以下是错误 - XMLHttpRequest 无法加载 finance.yahoo.com/webservice/v1/symbols/allcurrencies/…。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin 'runnerp14.codenvycorp.com:49782' 不允许访问。 testNamesCtrl.js:36 Object {data: null, status: 0, config: Object, statusText: ""}

标签: angularjs


【解决方案1】:

据我了解,您尝试发出跨域请求。您可以尝试为此使用$http.jsonp(url)。但只有在服务器允许使用JSONP 时才有效。

如我所见,这台服务器是不允许的。在这种情况下,您可以在您的服务器上准备数据。

echo file_get_contents(url);

Example;

【讨论】:

  • 非常感谢!
猜你喜欢
  • 2019-03-21
  • 2019-05-21
  • 2021-06-21
  • 1970-01-01
  • 2014-11-25
  • 2018-05-06
  • 1970-01-01
  • 2023-03-04
  • 1970-01-01
相关资源
最近更新 更多