X-Requested-With不允许访问控制允许标题 [英] X-Requested-With is not allowed by Access-Control-Allow-Headers

查看:145
本文介绍了X-Requested-With不允许访问控制允许标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个系统.在该系统中,购物车功能有一个添加项.在该功能中,我正在使用Jquery $ .ajax.但是在线服务器遇到了这个错误-

I am developing one system. In that system there is one add item to cart functionality. In that functionality, I am using Jquery $.ajax used. But online server I have facing this error -

"XMLHttpRequest无法加载域 名称/add_to_cart.php?item_id=3&hotel_id=2.请求标头字段 X-Requested-With是Access-Control-Allow-Header不允许的."

"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."

能帮我解决这个错误吗?

Can any help me how to solve this error.

我正在使用此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呼叫导致的服务器中....

in the server where ajax call leads to....

这篇关于X-Requested-With不允许访问控制允许标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆