我正在开发一个系统。在该系统中,有一项将商品添加到购物车的功能。在该功能中,我使用的是 Jquery $.ajax。但是在线服务器我遇到了这个错误 -
"XMLHttpRequest cannot load domain name/add_to_cart.php?item_id=3&hotel_id=2. Request header field X-Requested-With is not allowed by Access-Control-Allow-Headers."
能帮我解决这个错误吗
我正在使用这个 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 调用导致....的服务器中
关于javascript - Access-Control-Allow-Headers 不允许 X-Requested-With,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29344284/