Referrer header is not set in web requests

被刻印的时光 ゝ 提交于 2019-12-11 02:49:45

问题


When I load my web app via Phonegap and make a web request (via AJAX or otherwise), the REFERRER HTTP header is not set at all. This is interfering with the functionality of some third-party websites. How can I make the REFERRER header get sent?

(I am using Phonegap 3.5.0-0.20.10)


回答1:


As told by @JamesWong, now there is a plugin named cordovaHttp available for all platforms.

You can add that plugin using cordova plugin add https://github.com/wymsee/cordova-HTTP.git command and then you can add any http header as below.

cordovaHTTP.post("https://google.com/, {
    id: 12,
    message: "test"
}, { Authorization: "OAuth2: token" }, function(response) {
    // prints 200
    console.log(response.status);
    try {
        response.data = JSON.parse(response.data);
        // prints test
        console.log(response.data.message);
    } catch(e) {
        console.error("JSON parsing error");
    }
}, function(response) {
    // prints 403
    console.log(response.status);

    //prints Permission denied 
    console.log(response.error);
});

Source: https://github.com/wymsee/cordova-HTTP




回答2:


HTTP Requests are handled via webview/ chromeview (Android) and UIWebView (iOS). It is not possible to change it on JS/ HTML level. I reckon you might be able to achieve it by tweaking the Cordova layer, the down side to this is, you will have to do it for all the platforms you are supporting.

See this:

  • Add custom headers to WebView resource requests - android
  • Howto send a referer request in a url for a webView

You can probably write a plugin which interfaces with your JS/ HTML codes to determine when to send out the custom HTTP REFERRER.


EDIT

For iOS, there's a clean sample code with exactly what you need posted here:

  • Specifying HTTP referer in embedded UIWebView


来源:https://stackoverflow.com/questions/25111043/referrer-header-is-not-set-in-web-requests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!