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");
        }
        });
    
    评论

报告相同问题?

悬赏问题

  • ¥20 C#调用虚拟键盘TabTip.exe
  • ¥15 Qt4代码实现下面的界面
  • ¥15 CCS离散化传递函数与仿真不一致
  • ¥15 prism提示我reinstall prism 如何解决
  • ¥15 asp.core 权限控制怎么做,需要控制到每个方法
  • ¥20 while循环中OLED显示中断中的数据不正确
  • ¥15 这个视频里的stm32f4代码是怎么写的
  • ¥15 JNA调用DLL报堆栈溢出错误(0xC00000FD)
  • ¥15 请教SGeMs软件的使用
  • ¥15 自己用vb.net编写了一个dll文件,如何只给授权的用户使用这个dll文件进行打包编译,未授权用户不能进行打包编译操作?