Search code examples
ajaxcookiessencha-touch-2setcookie

Cookies not saved in Sencha Touch 2


I have a web service "someWebServiceOnServer" that sends back a json response and a Cookie. If I hit this URL through the browser, I see the following:

{"IsAuth":true,"Message":"","UserName":"guest"}

And if I inspect the page Resources -> Cookies, I see a Cookie set.

Now in my Sencha Touch 2 application, I am making an ajax call as follows:

Ext.Ajax.request({
url: 'someWebServiceOnServer',
useDefaultXhrHeader: false,
callback: function(options, success, response) {
console.log(response.responseText)
},
});

On running this, I get the JSON response as expected. But the cookie is not set. Also I cannot find the cookie in the response header. Why does this happen ?

Note: CORS has been implemented on the server and my application can access this service. I have not used withCredentials: true, since this throws an error XMLHttpRequest cannot load . Cannot use wildcard in Access-Control-Allow-Origin when credentials flag is true. .

Cookies are already enabled on my browser.

I need the cookie since I will be making subsequent calls that will send this cookie info back to the server.


Solution

  • Try this:

    Add the following headers to the server response.

    Access-Control-Allow-Origin: http://clientdomain.com
    Access-Control-Allow-Credentials: true

    And add withCredentials: true in the Ajax request.