Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 后续JSONP请求给出状态404,尽管GET状态为200_Javascript_Angularjs_Asynchronous_Jsonp_Yelp - Fatal编程技术网

Javascript 后续JSONP请求给出状态404,尽管GET状态为200

Javascript 后续JSONP请求给出状态404,尽管GET状态为200,javascript,angularjs,asynchronous,jsonp,yelp,Javascript,Angularjs,Asynchronous,Jsonp,Yelp,因此,我能够向yelpapi发出第一个JSONP请求,以便为我返回业务数据,但是我发出的任何后续请求都会导致对失败的回调,并记录404的状态代码。然而,当我在Chrome开发工具中打开网络选项卡时,我看到所有后续的GET请求都具有状态200,并且我确实看到了有效的响应数据。当调用失败回调时,这怎么可能呢?我怎样才能解决这个问题?我希望能够多次接收数据 下面讨论的代码基于对另一个问题的回答,不同之处在于注释正下方的语句 <!DOCTYPE html> <html> <

因此,我能够向yelpapi发出第一个JSONP请求,以便为我返回业务数据,但是我发出的任何后续请求都会导致对失败的回调,并记录404的状态代码。然而,当我在Chrome开发工具中打开网络选项卡时,我看到所有后续的GET请求都具有状态200,并且我确实看到了有效的响应数据。当调用失败回调时,这怎么可能呢?我怎样才能解决这个问题?我希望能够多次接收数据

下面讨论的代码基于对另一个问题的回答,不同之处在于注释正下方的语句

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
    <script src="oauth-signature.js"></script>
</head>
<body ng-app="plunker">
    <div  ng-controller="MainCtrl">
        <p><date-input name="info.name" message="info.message"></date-input></p>
        <ul>
            <li data-ng-repeat="business in businesses">
                {{business.name}}
            </li>
        </ul>
    </div>
    <script>
        function randomString(length, chars) {
            var result = '';
            for (var i = length; i > 0; --i) result += chars[Math.round(Math.random() * (chars.length - 1))];
            return result;
        }

        var app = angular.module('plunker', []);
        app.controller('MainCtrl', ['$scope', 'MyYelpAPI', function($scope, MyYelpAPI) {
            $scope.businesses = [];

            // first time for the callback gets executed
            MyYelpAPI.retrieveYelp('', function(data) {
                console.log('callback1');
                console.log('data');
                $scope.businesses = data.businesses;
            });

            // the second time for the callback doesn't get executed
            MyYelpAPI.retrieveYelp('', function(data) {
                console.log('callback2');
                $scope.businesses = data.businesses;
            });

        }]).factory("MyYelpAPI", function($http) {
            return {
                "retrieveYelp": function(name, callback) {
                    var method = 'GET';
                    var url = 'http://api.yelp.com/v2/search';
                    var params = {
                            callback: 'angular.callbacks._0',
                            location: 'San+Francisco',
                            oauth_consumer_key: '', 
                            oauth_token: '', 
                            oauth_signature_method: "HMAC-SHA1",
                            oauth_timestamp: new Date().getTime(),
                            oauth_nonce: randomString(32, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'),
                            term: 'food'
                        };
                    var consumerSecret = '',
                    var tokenSecret = '',
                    var signature = oauthSignature.generate(method, url, params, consumerSecret, tokenSecret, { encodeSignature: false});
                    params['oauth_signature'] = signature;
                    $http.jsonp(url, {params: params})
                        .success(callback)
                        // second time fails
                        // data -> undefined; status -> 404
                        .error(function(data, status) {
                            console.log(data, status); // why is this happening?
                        });
                }
            }
        });
    </script>
</body>
</html>

  • {{business.name}
函数随机字符串(长度、字符){ var结果=“”; 对于(var i=length;i>0;--i)result+=chars[Math.round(Math.random()*(chars.length-1)); 返回结果; } var app=angular.module('plunker',[]); app.controller('MainCtrl',['$scope','MyYelpAPI',函数($scope,MyYelpAPI){ $scope.businesss=[]; //第一次执行回调 MyYelpap.retrieveYelp(“”,函数(数据){ console.log('callback1'); console.log(“数据”); $scope.business=data.business; }); //第二次回调没有执行 MyYelpap.retrieveYelp(“”,函数(数据){ console.log('callback2'); $scope.business=data.business; }); }]).factory(“MyYelpAPI”,函数($http){ 返回{ “retrieveYelp”:函数(名称、回调){ var方法='GET'; var url='1〕http://api.yelp.com/v2/search'; 变量参数={ 回调:'angular.callbacks.\u 0', 地点:“旧金山”, oauth_消费者_密钥:“”, oauth_令牌:“”, oauth_签名_方法:“HMAC-SHA1”, oauth_时间戳:新日期().getTime(), oauth_nonce:randomString(32,'0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvxyz'), 术语:“食物” }; var consumerSecret='', var tokenSecret='', var signature=oauthSignature.generate(方法、url、参数、ConsumerCret、tokenSecret、{encodeSignature:false}); 参数['oauth_签名']=签名; $http.jsonp(url,{params:params}) .成功(回拨) //第二次失败 //数据->未定义;状态->404 .错误(功能(数据、状态){ console.log(数据、状态);//为什么会发生这种情况? }); } } });
您应该这样做:

$http.jsonp("api.yelp.com/v2/search?callback=JSON_CALLBACK", yourParams) 
.success(function() {
     //success callback
})
.error(function(){
     //error callback
}) ;

我想你需要这样做$http.jsonp(“,params).success(function(){//success}).error(function(){//failure});将回调查询参数添加到url中就成功了。谢谢!