在 ajax 中添加跨源标头,不起作用
add header in ajax for Cross-Origin, not working
当我在 ajax 中添加标头HTTP_X_REQUESTED_WITH
以请求另一台服务器时,它会给出 erorr 作为Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://www.xxxxxxxxxxxx.com/checkurl.php?action=xxxxxxx. This can be fixed by moving the resource to the same domain or enabling CORS.
如果我删除此标头,它可以正常工作。
我有很多 ajax 请求,所以我使用这种格式在所有 ajax 请求中添加标头
$(document).ajaxSend(function (event, jqxhr, settings) {
jqxhr.setRequestHeader('HTTP_X_REQUESTED_WITH', 'XMLHttpRequest')
});
我的 ajax 是:
$.ajax({
url:sitePath+'Xxxxxxxxx/checkurl.php?action=Pages&page='+actionPage,
type :'POST',
crossDomain:true,
success : function(data){
hideLoadingDiv();
if(data.getElementsByTagName("message")[0].getElementsByTagName("messageType")[0].childNodes[0].nodeValue=="SUCCESS")
{
document.getElementById(divID).innerHTML=data.getElementsByTagName("PageBody")[0].childNodes[0].nodeValue+'<span><a onclick="return checkRegistrationValidation();" href="javascript:void(0);" class="orengeBtn">Back</a></span>';
displayDiv(divID);
}
else if(data.getElementsByTagName("message")[0].getElementsByTagName("messageType")[0].childNodes[0].nodeValue=="ERROR")
{
showErrorMessage(data.getElementsByTagName("message")[0].getElementsByTagName("messageText")[0].childNodes[0].nodeValue);
}
else
{
generalError();
}
},
error:function(xhr,ajaxOptions, thrownError){
hideLoadingDiv();
if(xhr.status==200){
generalError();
}
else{
networkError();
}
if(debugMode==1){
displayAjaxError(xhr,thrownError);
}
}
});
在我的服务器文件中,我使用
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods: GET, POST');
header('Access-Control-Allow-Headers: X-Requested-With, Content-Type')
但是每次添加标题时都会出错。并在删除后正常工作。请帮忙。
你可以在这里找到答案:
跨域 AJAX 不发送 X-Request-With 标头
您必须在 ajax 中手动添加标头:
$.ajax({ url:sitePath+'Xxxxxxxxx/checkurl.php?action=Pages&page='+actionPage, type :'POST', crossDomain:true, headers: {'X-Requested-With': 'XMLHttpRequest'}
或者使请求不跨域:
$.ajax({ url:sitePath+'Xxxxxxxxx/checkurl.php?action=Pages&page='+actionPage, type :'POST', crossDomain:false,
相关文章:
- 带有.change()的AJAX不起作用
- 加载后的页面与ajax表单提交不起作用
- Ajax模式下的Jquery Select2 V4在IE9中不起作用
- curl和fileget_contents在ajax调用后不起作用
- Ajax在JQuery表单插件中不起作用
- 为什么 AJAX POST 请求不起作用
- javascript验证和ajax调用同时不起作用
- 使用Ajax的PHP_POST不起作用
- 另一个Ajax函数触发的Ajax函数不起作用
- $(select).selectmenu('打开')不'如果使用ajax加载页面,则不起作用
- AJAX上传文件不起作用
- Ajax调用不起作用
- Ajax:Ajax响应中的链接不起作用
- HtmlBox(jquery插件)在ajax调用中不起作用
- AJAX请求不起作用,除非使用alert()
- codeigniter ajax中的gzip压缩不起作用
- Ajax Post请求不起作用
- IE8 jquery ajax头请求不起作用
- 在同一个按钮上调用多个函数对我来说不起作用(ajax)
- jQuery验证submitHandler在$中不起作用.Ajax post表单数据