当 url 来自不同的服务器时,jQuery.ajax 失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1201429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery.ajax fails when url is from different server
提问by KingErroneous
Why does jQuery.ajax() throw an error with no error message if you use a URL with a dfferent server?
如果您将 URL 与不同的服务器一起使用,为什么 jQuery.ajax() 会抛出一个没有错误消息的错误?
采纳答案by tvanfosson
Its because of the restriction on cross domain requests implemented in the browser for XMLHttpRequests. You can get around this by using JSONP as the format, otherwise you'll need a server-side proxy for the request.
这是因为 XMLHttpRequests 在浏览器中实现的跨域请求的限制。您可以通过使用 JSONP 作为格式来解决这个问题,否则您将需要一个服务器端代理来处理请求。
Quoting from the ajax documentationon http://jquery.com
Note: All remote (not on the same domain) requests should be specified as GET when 'script' or 'jsonp' is the dataType (because it loads script using a DOM script tag). Ajax options that require an XMLHttpRequest object are not available for these requests. The complete and success functions are called on completion, but do not receive an XHR object; the beforeSend and dataFilter functions are not called.
注意:当 'script' 或 'jsonp' 是 dataType 时,所有远程(不在同一个域中)请求都应该指定为 GET(因为它使用 DOM 脚本标签加载脚本)。需要 XMLHttpRequest 对象的 Ajax 选项不适用于这些请求。complete 和 success 函数在完成时被调用,但不接收 XHR 对象;不会调用 beforeSend 和 dataFilter 函数。
回答by Andrey Volk
As http://en.wikipedia.org/wiki/Cross-origin_resource_sharingsays:
正如http://en.wikipedia.org/wiki/Cross-origin_resource_sharing所说:
Cross-origin resource sharing (CORS)is a mechanism that allows a web page to make XMLHttpRequests to another domain.1Such "cross-domain" requests would otherwise be forbidden by web browsers, per the same origin security policy. CORS defines a way in which the browser and the server can interact to determine whether or not to allow the cross-origin request.2It is more powerful than only allowing same-origin requests, but it is more secure than simply allowing all such cross-origin requests.
跨域资源共享 (CORS)是一种机制,允许网页向另一个域发出 XMLHttpRequest。1否则,根据同源安全策略,Web 浏览器将禁止此类“跨域”请求。CORS 定义了一种浏览器和服务器可以交互的方式来确定是否允许跨域请求。2它比只允许同源请求更强大,但比简单地允许所有此类跨域请求更安全。
For PHP it is done using header()function:
对于 PHP,它是使用header()函数完成的:
<?php
header("Access-Control-Allow-Origin: http://example.com");
?>
CORScan be used as a modern alternative to the JSONP pattern. While JSONP supports only the GET request method, CORS also supports other types of HTTP requests. Using CORS enables a web programmer to use regular XMLHttpRequest, which supports better error handling than JSONP. On the other hand, JSONP works on legacy browsers which preclude CORS support. CORS is supported by most modern web browsers. Also, whilst JSONP can cause XSS issues where the external site is compromised, CORS allows websites to manually parse responses to ensure security.
JSONP or "JSON with padding" is a communication technique used in JavaScript. It provides a method to request data from a server in a different domain, something prohibited by typical web browsers because of the same origin policy.
CORS可以用作 JSONP 模式的现代替代方案。虽然 JSONP 仅支持 GET 请求方法,但 CORS 还支持其他类型的 HTTP 请求。使用 CORS 使 Web 程序员能够使用常规 XMLHttpRequest,它支持比 JSONP 更好的错误处理。另一方面,JSONP 适用于排除 CORS 支持的旧版浏览器。大多数现代 Web 浏览器都支持 CORS。此外,虽然 JSONP 可能导致外部站点受到威胁的 XSS 问题,但 CORS 允许网站手动解析响应以确保安全。
JSONP 或“带填充的 JSON”是 JavaScript 中使用的一种通信技术。它提供了一种从不同域中的服务器请求数据的方法,由于同源策略,典型的 Web 浏览器禁止这样做。
回答by Bogdan
The ajax() method internally uses XmlHttpRequest which obeys the same domain policy http://en.wikipedia.org/wiki/Same_origin_policy. The getJson()method can be used instead for making cross domain calls.
ajax() 方法在内部使用 XmlHttpRequest,它遵守相同的域策略http://en.wikipedia.org/wiki/Same_origin_policy。所述的getJSON()方法可用于代替用于制备跨域呼叫。
I hope this helps, Bogdan
我希望这会有所帮助,博格丹
回答by max
Because if Same Origin Policy jQuery will not allow this. The best option will be using some proxy server page to get the required pages.
因为如果同源策略 jQuery 将不允许这样做。最好的选择是使用一些代理服务器页面来获取所需的页面。