weixin_33737774 2015-02-13 05:19 采纳率: 0%
浏览 47

强制更改AJAX标头

So I am developing a Apache Cordova app for Android, and I'd like to be able to set the headers for the AJAX requests I am sending, including the Host, Origin, and Referer fields. Because these are being executed in a separate WebView, I can't access the user's original cookies or sessions (I'm not being malicious, I promise). But whenever I attempt to send that information, I get a "Refused to set unsafe header Host" error. Is there any way to override this in Apache Cordova?

  • 写回答

2条回答 默认 最新

  • weixin_33696106 2015-02-20 02:25
    关注

    If you are using jQuery, you can try using the headers option when setting up the call. You can also use the beforeSend option if you need more fine-grained control over the headers.

    //set specific accept headers (should work cross browser)
    $.ajax({
    headers: {
        Accept : "text/plain; charset=utf-8",
        "Content-Type": "text/plain; charset=utf-8"
    }
    });
    
    //an alternative to the above
    $.ajax({
        beforeSend: function(xhrObj)
        {
            xhrObj.setRequestHeader("Content-Type","application/json");
            xhrObj.setRequestHeader("Accept","application/json");
        }
        });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条