Yas*_*ila 5 javascript apache ajax google-chrome cors
我正在尝试使用CORS在我的浏览器和Apache服务器(驻留在不同的域中)之间运行AJAX请求.
在服务器端,我在服务器的httpd.conf部分中进行了以下更改,根据" Header set Access-Control-Allow-Origin in .htaccess中的响应不起作用 ":
Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
Run Code Online (Sandbox Code Playgroud)
我的AJAX调用形式如下:
$.ajax({
url :'https://x.x.x.x/validateCustomerID',
type : 'POST',
cache : false,
crossDomain: true,
contentType: 'application/json',
beforeSend: function(xhr){
xhr.setRequestHeader("Access-Control-Allow-Methods","POST");
xhr.setRequestHeader("Access-Control-Allow-Headers","X-Requested-With");
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
},
data : {loginId : '12345'},
success : function(response){console.log("Success"+JSON.stringify(response))},
error : function(response){console.log("Error"+JSON.stringify(response))}
});
}
Run Code Online (Sandbox Code Playgroud)
我也试过注释掉beforeSend()以避免预检请求,但它也没有成功.
我在Chrome和Firefox上收到的错误消息是:
"XMLHttpRequest无法加载https:// xxxx/validateCustomerID.请求的资源上不存在'Access-Control-Allow-Origin'标头.因此不允许原点'null'访问.响应的HTTP状态代码为403."
"阻止跨源请求:同源策略禁止在https:// xxxx/validateCustomerID读取远程资源.(原因:CORS请求失败)."
我的浏览器中没有从服务器收到响应标头,我认为这是CORS必须工作的,并且服务器中的日志显示没有请求从我的浏览器到达它.
我真的很感激,如果有人在这里可以帮助我解决这个问题,因为我已经在这里呆了几天,并且已经使用了几乎所有的命中和试用方法来使这个工作成功.
这是我的设置,site.conf
现在可以在生产中使用 apache2
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Headers "authorization, origin, user-token, x-requested-with, content-type"
Header set Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
Run Code Online (Sandbox Code Playgroud)
为了将来参考,我强烈建议将此网站添加为书签http://enable-cors.org/index.html
归档时间: |
|
查看次数: |
3304 次 |
最近记录: |