javascript Chrome 显示访问控制允许来源错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19001107/
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
Chrome shows Access Control Allow Origin error
提问by Edgar
I'm using jquery to post data to a server, the server received the data, but the chrome still shows the error :
我正在使用 jquery 将数据发布到服务器,服务器收到了数据,但 chrome 仍然显示错误:
XMLHttpRequest cannot load `myServer:63373/api/sendData`. Origin `myServer:63385` is not allowed by Access-Control-Allow-Origin.
Here is my js code:
这是我的js代码:
$.ajax({
type: 'POST',
url: 'myServer:63373/api/SendData',
crossdomain: true,
async: true,
data: myData,
dataType: 'json',
success: function(){ alert('success'); }
});
The strange thing is that the 'success' shows up after the request, and the server did receive the data, but then the error message shows up in the console. Is there any way to avoid this message?
奇怪的是,请求后显示“成功”,服务器确实收到了数据,但随后在控制台中显示了错误消息。有没有办法避免这个消息?
回答by Edgar
Thanks for all you answers above but I think I've found the way, before I solve this I was trying to use two ways, Setting Access-Control-Allow-Origin in ASP.Net MVC - simplest possible methodwith the 87 votes and 32 votes, I found the answers at two different places, and both applied them, but it didn't work. Early this week, I tried to delete the code in the web.config and just left the code in C#, it works! Can't believe these two solutions have confliction.
感谢您提供上述所有答案,但我想我已经找到了方法,在解决此问题之前,我尝试使用两种方法,在 ASP.Net MVC 中设置访问控制允许源 - 最简单的方法,获得 87 票和32票,我在两个不同的地方找到了答案,都应用了,但是没有用。本周早些时候,我尝试删除 web.config 中的代码,并将代码留在 C# 中,它有效!不敢相信这两种解决方案有冲突。
Anyway, hope this could help others.
无论如何,希望这可以帮助其他人。
回答by David-SkyMesh
"not allowed by Access-Control-Allow-Origin" is the reason. Put simply, you can't normally do an XHR call to a different domain/port/protocol from those of the webpage your javascript was included into.
“Access-Control-Allow-Origin 不允许”是原因。简而言之,您通常无法对不同的域/端口/协议进行 XHR 调用,而这些域/端口/协议与您的 javascript 包含在其中的网页的域/端口/协议不同。
Modern browsers allow you work around this with permission from the server administrator.
现代浏览器允许您在获得服务器管理员许可的情况下解决此问题。
You make your Ajax request with the XHR2 API (http://www.html5rocks.com/en/tutorials/cors/)
您使用 XHR2 API ( http://www.html5rocks.com/en/tutorials/cors/)发出 Ajax 请求
The on the server side, you need to emit the following headers to allow access from the domain that served the page that included your javascript:
在服务器端,您需要发出以下标头以允许从为包含您的 javascript 的页面提供服务的域进行访问:
Access-Control-Allow-Origin: http://your-page-domain.com
Access-Control-Allow-Credentials: true
回答by developerCK
if situation are
如果情况是
your script is hosted on another server
or subdomain
or try to hit url with another port
您的脚本托管在另一台服务器上
或子域
或尝试使用另一个端口访问 url
this is cross-domian request. jquery can not fulfill cross-domain request.
这是跨域请求。jquery 无法完成跨域请求。
if this is your issue , then you should use jsonp for this "jsonp " allows cross domain request.
如果这是您的问题,那么您应该将 jsonp 用于此“jsonp”允许跨域请求。
try to do this with using jsonp.
尝试使用jsonp来做到这一点。
for jsonp every call has a callback means there will be a value in url and you have to send respnse with this call back in php
对于 jsonp,每个调用都有一个回调,这意味着 url 中会有一个值,您必须在 php 中发送带有此调用的响应
means try this:-
意味着试试这个:-
$.ajax({
type: 'POST',
url: 'myServer:63373/api/SendData',
crossdomain: true,
async: true,
data: myData,
dataType: 'jsonp',
success: function(){ alert('success'); }
});
and in php
并在 php 中
wrap your response in $_GET['_callback']."(".$response.")"; and echo it
you will get response in json .
你会得到 json 的响应。