尽管GET状态为200,但后续JSONP请求给出状态404 [英] Subsequent JSONP requests give status 404 despite GET status 200

查看:72
本文介绍了尽管GET状态为200,但后续JSONP请求给出状态404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I am able to make the first JSONP request to the yelp API work to return business data for me, but any subsequent requests I make lead to the callback for failure that logs a status code of 404. Yet when I pull up the network tab in Chrome Dev Tools I see that all the subsequent GET requests I made all have status 200 and I do see valid response data. How can that be when the callback for failure was called? How can I fix this? I'd like to be able to receive data more than once.

以下代码基于对这个问题的答案支持API和AngularJS ,区别在于评论正下方的陈述

The following code in question is based off the answer to this other question Yelp API and AngularJS, the difference being the statements directly below the comments

<!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>


推荐答案

你应该这样做:

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

这篇关于尽管GET状态为200,但后续JSONP请求给出状态404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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