jqXHR - http-status-code-403(但状态码为0) [英] jqXHR - http-status-code-403 (but the statuscode is 0)

查看:187
本文介绍了jqXHR - http-status-code-403(但状态码为0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到状态代码0 ......但它是代码403.
有人可以告诉我这是什么问题吗?

i get the statuscode 0 ... but it is the code 403. Can someone tell me what the problem is?

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;
    })

DEMO - > http://jsfiddle.net/QFuBr/

提前致谢!

Peter

Thanks in advance!
Peter

推荐答案

服务器向浏览器发出403错误,因为您无权访问该资源,因为报告错误消息(请求的用户的收藏夹不公开。)。

The server gives a 403 error to a browser, because you don't have permission to access the resource, because of the error message reported ("Favorites of requested user are not public.").

但是,服务器甚至没有在jsFiddle示例中获取请求。

However, the server doesn't even get the request in the jsFiddle example.

您不能进行跨浏览器的AJAX请求。这称为同源政策。出于安全原因,防止恶意程序员在您不知情的情况下做出不愉快的事情。这是一个生硬的工具,但却是一个有效的工具。

You aren't allowed to make cross-browser AJAX requests. This is called the same-origin policy. It is for security reasons, to prevent malicious coders from doing unpleasant things without your knowledge. It's a blunt tool, but an effective one.

当你甚至没有向服务器发送请求时,没有状态代码。这由XMLHTTPRequest对象(及其jqXHR包装器)报告为 0

When you don't even get as far as sending a request to the server, there is no status code. This gets reported by the XMLHTTPRequest object (and its jqXHR wrapper) as 0.

基本上,你不能做你想在浏览器中做的事情。

Basically, you can't do what you're trying to do in the browser.

如果你需要浏览器异步访问这样的数据,你需要在你的服务器上写一个包装器从远程服务器获取信息并将其提供给浏览器。有一种解决方法(它叫做JSONP– JSON with Padding)但我不相信YouTube支持它。

If you need the browser to access data like this asynchronously, you'll need to write a wrapper on your server to fetch the information from the remote server and feed it to the browser. There is a workaround (it's called JSONP – JSON with Padding) but I don't believe YouTube supports it.

编辑:每 gradbot的回答,可以通过更改代码将 dataType 设置为 jsonp

Per gradbot's answer, it is possible to do a JSONP request by changing your code to set dataType to jsonp.

但是,您现在无法使用 xhr.status 。这是因为JSONP不使用XHR对象,因此没有可供检查的状态。

However, you won't now be able to use xhr.status. This is because JSONP does not use the XHR object, so there is no status available to check.

这是使用建议的饲料gradbot 的工作示例。请注意,结果对象将传递给处理程序,而不是jqXHR对象。

Here's a working example using the feed gradbot suggested. Note that the result object is passed to the handler, rather than the jqXHR object.

这篇关于jqXHR - http-status-code-403(但状态码为0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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