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

How to support cookie in WKWebView #197

Open martatw opened 8 years ago

martatw commented 8 years ago

Hi,

After adding this plugin in order to migrate cordova app from UIWebView to WKWebView, I have a problem with cookie. My apps, after successfull authentication made by ajax call, receives cookie that should add to the next ajax call. This worked in UIWebView, but after migration to WKWV I get not authorized exception. How can I get cookie from server response and add it to the ajax call?

document.cookie returns me empty string

andreialecu commented 8 years ago

You may need to enable the withCredentials flag in order to use cookies since this plugin sets up a HTTP server on localhost, and CORS is enforced. See https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Requests_with_credentials

martatw commented 8 years ago

Hi,

I use ajax as above:

    return AjaxJson({
        url: XXX + "/config/resources.json",
        xhrFields: { "withCredentials": true},
        beforeSend: function() { AppSett.showLoading("loading resources..."); },
        complete: function() { AppSett.hideLoading(); },
        error: function(xhr, textStatus, error) {
            _handleErrorMsg(xhr, textStatus, error);
        }
    });

AjaxJson = function (options) { var ajaxOptions = $.extend({ type: "GET", contentType: "application/json; charset=utf-8", dataType: "json", crossDomain: true }, options); return $.ajax(ajaxOptions); };

It works fine on browser platform, but in iOS platform => iOS simulator I get unauthorized exception. I have looked at safari=>developer=>simulator=>index.html => network and in the request there was no cookie.

rayshan commented 8 years ago

I'm running into the same issue with 0.6.8 - first request with basic auth and xhr.withCredentials = true is successful, response header includes Set-Cookie; second request fails, error message is:

XMLHttpRequest cannot load http://xxx/.../get_user_roles. Origin http://localhost:12344 is not allowed by Access-Control-Allow-Origin.

In Safari Web Inspector -> Storage, Cookies section seems to be empty.

andreialecu commented 8 years ago

@rayshan You need to set up CORS to allow http://localhost:12344 as an origin on your http://xxx/ server. Search for CORS here: http://plugins.telerik.com/cordova/plugin/wkwebview for pointers.

rayshan commented 8 years ago

Thanks @andreialecu. The server indeed doesn't send CORS headers on this endpoint. We're using http basic auth. On desktop browsers and in UIWebView, xhr.withCredentials = true works without CORS headers. But in WKWebView with this plugin it doesn't. We'll try to add CORS headers to see if it helps.

karosLi commented 7 years ago

how about this issue guys. I also meet this issue that ajax request will lose cookie. Any body resolve it?

FarhadG commented 5 years ago

I'm having the same issue, unfortunately. Any luck with a resolution?