Telerik-Verified-Plugins / WKWebView

DEPRECATED - this plugin served a purpose in the past, but there are better implementation now
832 stars 149 forks source link

Setting request headers breaks requests. #142

Open ronaldjeremy opened 9 years ago

ronaldjeremy commented 9 years ago

Consider the following to set a request header:

$.ajax("http://somedomain/somepage", {headers: {testheader: "something"}})

I can see it is making an OPTIONS preflight request, but after that it never makes the second GET request. The request just breaks.

I have CORS configured properly on the server end and it includes the following in the response:

Access-Control-Allow-Headers:Authorization, Content-Type, testheader Access-Control-Allow-Methods:GET, POST, OPTIONS Access-Control-Allow-Origin:http://localhost:12344

The request without the custom header works, the request with the custom header works in UIWebView, but no matter what I have tried custom request headers always cause the request to fail under WKWebView

fleather commented 9 years ago

I experienced the same. I did this:

cordova create test
cd test
cordova platform add ios
cordova plugin add plugin-cordova-wkwebview

cordova plugin ls

>> com.telerik.plugins.wkwebview 0.6.0 "WKWebView Polyfill"
>> cordova-plugin-webserver 1.0.3 "CordovaWebServer"
>> cordova-plugin-whitelist 1.0.0 "Whitelist"

cordova prepare

And then I wrote the following piece of code after "deviceready" event:

        function loadListener () {
          alert(this.responseText);
        }

        var xhr = new XMLHttpRequest();
        xhr.addEventListener("load", loadListener);
        xhr.open("GET", "http://localhost:12344/file.txt");
        xhr.setRequestHeader('Content-Type', 'text/plain; charset=utf-8');
        xhr.send();

Commenting the line makes the code work:

        // xhr.setRequestHeader('Content-Type', 'text/plain; charset=utf-8');

Is there anything I can do? (Besides leaving out the line of code)

Thank you very much in advance!