我正在开发一个系统。在该系统中,有一个向购物车添加项目的功能。在该功能中,我使用的是Jquery$.ajax。但是在线服务器我面临这个错误-
"XMLHttpRequest无法加载域name/add_to_cart.php?item_id=3&hotel_id=2.请求标头字段访问控制允许标头不允许X-Requested-Wise。"
有谁能帮我解决这个错误吗。
我正在使用这个jquery代码
$(document).on('click', '.ordering_btn', function(){
var item_id = $(this).data('value');
var hotel_id = "<?php echo $hotel_id; ?>";
$.ajax({
type: 'GET',
url: 'add_to_cart.php?item_id='+item_id+'&hotel_id='+hotel_id+'',
contentType: 'text/plain',
xhrFields: {
withCredentials: false
},
headers: {
"Access-Control-Allow-Headers": "X-Requested-With",
"X-Requested-With": "XMLHttpRequest"
},
success: function(data) {
$('#cart_msg').css('display', 'none');
$('#cart_item').html(data);
console.log(data);
},
error: function() {
}
});
});
可以通过添加来修复错误
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');
在ajax调用导致…的服务器中
删除此:
headers: {
"Access-Control-Allow-Headers": "X-Requested-With",
"X-Requested-With": "XMLHttpRequest"
},
Access-Control-Allow-Headers
是响应标头,而不是请求标头。
您请求的服务器不允许X-Requested-With
。