jqXHR - http-status-code-403 (but the statuscode is 0)
jqXHR - http-status-code-403 (but the statuscode is 0)
我得到statuscode 0…但是代码是403。谁能告诉我是什么问题吗?
JQUERY var jqxhr = $.ajax({
url: 'http://gdata.youtube.com/feeds/api/users/bernd/favorites?alt=json',
dataType: 'json'
}).success(function(xhr) {
alert(xhr.status);
}).error(function(xhr) {
alert(xhr.status);
return false;
})
-> http://jsfiddle.net/QFuBr/
提前感谢!
彼得。
服务器给出一个403错误给浏览器,因为您没有访问资源的权限,因为报告了错误消息("请求用户的收藏夹不是公共的")。
但是,在jsFiddle示例中,服务器甚至没有获取请求。
不允许跨浏览器的AJAX请求。这被称为同源策略。这是出于安全原因,防止恶意程序员在你不知情的情况下做一些不愉快的事情。这是一个生硬的工具,但却是一个有效的工具。
如果您甚至没有向服务器发送请求,则没有状态码。这由XMLHTTPRequest对象(及其jqXHR包装器)报告为0
。
基本上,你不能在浏览器中做你想做的事情。
如果需要浏览器以这种异步方式访问数据,则需要在服务器上编写包装器,以便从远程服务器获取信息并将其提供给浏览器。有一个解决方案(它被称为JSONP & &;JSON with Padding) 但是我不相信YouTube支持它
编辑:根据gradbot的答案,可以通过更改代码将dataType
设置为jsonp
来执行JSONP请求。
然而,你现在将不能使用xhr.status
。这是因为JSONP不使用XHR对象,所以没有可用的状态检查。
下面是一个使用gradbot建议的提要的工作示例。注意,结果对象被传递给处理程序,而不是jqXHR对象。
您需要设置dataType: "jsonp"
,并且您需要作为您试图从中获取收藏的用户登录。在本例中,我使用自己的用户名grabot,警报返回成功。
如果你没有一个有效的cookie为您试图访问的帐户,那么api调用将返回403
与内容"Favorites of requested user are not public."
$(function() {
var jqxhr = $.ajax({
url: 'http://gdata.youtube.com/feeds/api/users/gradbot/favorites?alt=json',
dataType: 'jsonp'
}).success(function(data, status) {
alert(status);
}).error(function(xhr) {
alert(xhr.status);
})
});
403是因为您需要为正在访问其视频的用户提供凭据。假设提供了正确的凭据,由于跨域限制,请求仍然会失败。
在大多数情况下,状态码0
意味着请求不能被发送到服务器。以下是Chrome控制台日志显示的小提琴示例。
XMLHttpRequest无法加载http://gdata.youtube.com/feeds/api/users/bernd/favorites?alt=json。Access-Control-Allow-Origin不允许Origin http://fiddle.jshell.net
Youtube,事实上所有的Google Data API都支持JSONP,但是要使用它,你必须传递一个alt
参数,值为json-in-script
,并指定dataType
为jsonp
。jQuery将为您提供回调参数。根据实证测试,Youtube似乎并不关心alt
参数具体是json-in-script
。只要指定了callback
参数,alt
参数只能取json
值。
http://gdata.youtube.com/feeds/api/users/gradbot/favorites?alt=jsonhttp://gdata.youtube.com/feeds/api/users/gradbot/favorites?alt=json&调= foo
这是一个公开访问提要的工作示例。
$.ajax({
url: 'http://gdata.youtube.com/feeds/mobile/videos?alt=json-in-script',
dataType: 'jsonp',
success: function(data) {
// do something with data
}
});
由于大多数现代浏览器的安全限制,您不能进行跨域请求(无论是GET还是POST)。
如果您仍然想从其他域获取数据,请考虑使用您安装在服务器上的反向代理并通过该代理发送所有请求。对于浏览器来说,它仍然看起来像来自同一个域的数据。
最流行的一个是Apache中的mod_reverse,但是根据您的服务器环境还有其他选择。
- Is onfling available for html, html5
- jQuery is not loaded
- reactRedux is not defined
- ReferenceError: cordova is not defined @ng-cordova.min.js:7
- Jquery - table.row(tr) is undefined
- contentWindow.document.body is null
- Tomcat websocket is not working
- noty.js is returning undefined
- $routeParams is undefined
- NodeJS Multer is not working
- React, Webpack: bundle.js is not generated
- jQuery is not defined - WordPress footer.php
- 谷歌地图API v3:Initial View is Fine,but Gray Box with No Map if
- imageArray[i] = imageItem is incompatible
- 为什么Jquery$.ajax在函数调用中触发所有statusCode,即使调用成功
- GAPI Is Not Defined
- Angular2 rc1, where is shims_for_IE
- Is (!~index) faster than (index === -1)?
- Mouse Out Orange Div "CurrentHeight" is not applyi
- jqXHR - http-status-code-403 (but the statuscode is 0)