AngularJS RESTAPI调用抛出错误 [英] AngularJS RESTAPI call throwing error

查看:32
本文介绍了AngularJS RESTAPI调用抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm learning AngularJS. I was trying to make a RESTapi call using standard $http service but I'm not getting a successful response. Any clue what I'm doing wrong here?

我正在调用URL(当我对其进行ping操作时,URL可以正常显示): http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=json

I'm calling the URL (URL is rendering fine when I ping it): 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测试代码是:

My Angularjs test code is:

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(url).但是只有在服务器允许使用 JSONP 的情况下,此方法才有效.

As I understand, You try make cross domain request. You can try use $http.jsonp(url) for this. But it was work only if server allows use JSONP.

据我所见,此服务器不允许.在这种情况下,您可以在服务器上准备数据.

And as I see, this server not allowed. In this case, you can prepare data on Your server.

echo file_get_contents(url);

示例;

这篇关于AngularJS RESTAPI调用抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆