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");
        }
        });
    
    评论
  • weixin_33711641 2015-02-20 07:39
    关注

    You have to use a plugin that makes the request from native side. This approach will allow you to bypass the web view limitations on making request outside your origin.

    You could start using this plugin: https://github.com/wymsee/cordova-HTTP.

    评论

报告相同问题?

悬赏问题

  • ¥65 页面调接口时加载卡住不响应
  • ¥35 用C语言解决编程问题
  • ¥15 unity硬件连接与使用
  • ¥15 鱼缸加热棒的数据分析或者实际案例也行
  • ¥15 postgresql11安装完成后,pgadmin无法启动
  • ¥15 (标签-无人机|关键词-Matlab代码)
  • ¥15 执行shell脚本提示参数太多
  • ¥15 困扰好久了,电脑的串口调试助手向单片机发送一个字符(如:字符‘1’到‘8’),单片机点亮对应的灯(如发送字符‘2’则点亮第2个灯)并给电脑回复信息:已点亮第x个灯(其中 x 要替换成对应的数字)
  • ¥15 求减少这个c++程序内存的解决方案
  • ¥30 改正代码,错误比较多