作者:宗瑋婉君珮璇 | 来源:互联网 | 2023-05-18 08:03
ImtryingtogetanAJAXrequestworkingbetweenmybrowserandanApacheserver(residinginadiff
I'm trying to get an AJAX request working between my browser and an Apache server(residing in a different domain) using CORS.
我正在尝试使用CORS在我的浏览器和Apache服务器(驻留在不同的域)之间运行AJAX请求。
At the server side, I've made the following changes in the httpd.conf section of the server as per the responses in "Header set Access-Control-Allow-Origin in .htaccess doesn't work":
在服务器端,我根据“Header set Access-Control-Allow-Origin in .htaccess中的响应不起作用”中的响应在服务器的httpd.conf部分进行了以下更改:
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"
My AJAX call is of the form:
我的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))}
});
}
I've also tried commenting out the beforeSend() in order to avoid a preflight request but it wasn't successful either.
我也试过注释掉beforeSend()以避免预检请求,但它也没有成功。
The error messages that I receive on Chrome and Firefox are:
我在Chrome和Firefox上收到的错误消息是:
"XMLHttpRequest cannot load https://x.x.x.x/validateCustomerID. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 403."
“XMLHttpRequest无法加载https://x.x.x.x/validateCustomerID。请求的资源上没有'Access-Control-Allow-Origin'标头。因此不允许原点'null'访问。响应的HTTP状态代码为403.”
"Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://x.x.x.x/validateCustomerID. (Reason: CORS request failed)."
“阻止跨源请求:同源策略禁止在https://x.x.x.x/validateCustomerID读取远程资源。(原因:CORS请求失败)。”
There are no response headers received from the server in my browser which I think are mandatory for CORS to work and also logs in the server shows no request reaching it from my browser.
在我的浏览器中没有从服务器收到响应标头,我认为这是CORS必须工作的,并且服务器中的日志显示没有请求从我的浏览器到达它。
I would really appreciate if someone here can help me resolve this issue as I'm stuck here for quite a few days now and have used almost all hit and trial methods to make this thing work.
我真的很感激,如果有人在这里可以帮助我解决这个问题,因为我现在已经在这里停留了几天,并且已经使用了几乎所有的命中和试用方法来使这个工作成功。
1 个解决方案