Javascript XMLHttpRequest; 跨源请求仅支持以下协议方案:http、data、chrome、chrome-extension、https、chrome-extension-resource
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32578707/
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
XMLHttpRequest; Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource
提问by Durgesh Pandey
When I try to do a ajax call, I got the error on chrome below.
当我尝试进行 ajax 调用时,我在下面的 chrome 上遇到了错误。
XMLHttpRequest cannot load javascript:;. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
XMLHttpRequest 无法加载 javascript:;。跨源请求仅支持以下协议方案:http、data、chrome、chrome-extension、https、chrome-extension-resource。
Here is code:
这是代码:
$.ajax({
type: "POST",
data: {pvalue : pid},
cache: false,
url: "xxx.in/yy/ajax.php",
success: function(data)
{
$modal.find('.edit-content').html(data);
}
回答by jfriend00
All research into that specific error message suggests that the host web page is not being loading via an http:// URL and is probably a file: URL. The browser will not, by default, permit cross origin requests from a file: URL.
对该特定错误消息的所有研究表明,主机网页不是通过 http:// URL 加载的,可能是一个文件:URL。默认情况下,浏览器不会允许来自 file: URL 的跨源请求。
You need to load the web page through your web server, not via the file system if you want to use ajax requests.
如果要使用 ajax 请求,则需要通过 Web 服务器加载网页,而不是通过文件系统。
Here are some other questions and answers about that specific error that all point to the wrong type of URL being used to load the page.
以下是有关该特定错误的其他一些问题和答案,这些问题和答案都指向用于加载页面的 URL 类型错误。
"Cross origin requests are only supported for HTTP." error when loading a local file
React.js: Example in tutorial not working
Cross origin requests are only supported for HTTP but it's not cross-domain
https://groups.google.com/forum/#!topic/tincr-for-chrome-devtools/nA9k2qh7F-g
https://groups.google.com/forum/#!topic/tincr-for-chrome-devtools/nA9k2qh7F-g
回答by Tismon Varghese
If you are accessing data from other domains, you have to override Chrome's Same-origin Policy. For that you have to specify dataType: 'jsonp'
如果您从其他域访问数据,则必须覆盖 Chrome 的Same-origin Policy. 为此,您必须指定dataType: 'jsonp'
$.ajax({
type: "POST",
dataType: 'jsonp',
data: {pvalue : pid},
cache: false,
url: "xxx.in/yy/ajax.php",
success: function(data)
{
$modal.find('.edit-content').html(data);
}
});
If the file ajax.phpin your server (the one which you are working now), you can simply specify the file name in urlsection (as below).
如果ajax.php您的服务器中的文件(您现在正在工作的那个),您可以简单地在url部分中指定文件名(如下所示)。
$.ajax({
type: "POST",
data: {pvalue : pid},
cache: false,
url: "ajax.php",
success: function(data)
{
$modal.find('.edit-content').html(data);
}
});

