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

编程入门 行业动态 更新时间:2024-10-07 12:18:36
本文介绍了尽管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="ajax.googleapis/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 = 'api.yelp/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/v2/search?callback=JSON_CALLBACK", yourParams) .success(function() { //success callback }) .error(function(){ //error callback }) ;

更多推荐

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

本文发布于:2023-10-10 18:23:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479368.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:状态   JSONP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!