2

I have a project which using a a REST API. Here when I send a login request, they are sending me the response as JSON containg some data. Along with that in Response Header

Access-Control-Allow-Origin:*
Cache-Control:private
Content-Encoding:gzip
Content-Length:49
Date:Sun, 28 Jul 2013 08:09:29 GMT
ntCoent-Length:36
Server:Apache-Coyote/1.1
Set-Cookie:JSESSIONID=044F94AFE2997F02475315474E3B822D; Path=/
Set-Cookie:NSC_Ibgja-IUUQt=ffffffffc3a0b60545525d5f4f58455e445a4a4229a0;expires=Sun, 28-Jul-2013 08:55:40 GMT;path=/;secure
X-Powered-By:Servlet 2.4; JBoss-4.2.2.GA (build: SVNTag=JBoss_4_2_2_GA              

Here we have a Set-Cookie , but this cookie is not setting. I need this cookie to be setted, because for any other API access, the server will check for this cookie.

How can I resolve this Issue? What is the solution for jQuery AJAX response Header Set-Cookie method?

Update:

the first ajax for login

    var json='{"nin":"'+1080258278+'","password":"'+840622+'"}';
var hostURL = "https://www.hafiz.gov.sa";
$.ajax({
    type: "POST",
    url: hostURL + "/HRDFWeb/AuthenticateDirectly",
    data: "json="+json+"&date="+Number(new Date()),
    dataType: "json",
    timeout: 180000, // in milliseconds
    success: function(data, textStatus, XMLHttpRequest){

        console.log(data);
   },
    error: function(request, status, err) {

    }
});
Cœur
  • 32,421
  • 21
  • 173
  • 232
Anh Hậu
  • 91
  • 1
  • 11
  • How do you know the Cookie isn't being set? How are you accessing the API? – Pekka Jul 28 '13 at 08:22
  • there are 2 `Set-Cookie` which would cause problems. – Khanh TO Jul 28 '13 at 08:22
  • @Khanh not really, seeing as they're for different cookie names – Pekka Jul 28 '13 at 08:24
  • @Pekka 웃: even they're for different cookie keys, we should combine into 1 `Set-Cookie` only. Otherwise, the cookie will be expired immediately (I had this problem in the past with IE). – Khanh TO Jul 28 '13 at 08:25
  • @Pekka웃 document.cookie has not values, i have 2 ajax call: 1 for login, and 1 for getting infomations. when i login, server return this reponse header – Anh Hậu Jul 28 '13 at 08:27
  • @KhanhTO i checked with Chrome and FireFox – Anh Hậu Jul 28 '13 at 08:28
  • I can't access to server, how can i resolve this issue – Anh Hậu Jul 28 '13 at 08:29
  • @Khanh it seems to be generally accepted and acceptable practice to send multiple cookie headers - even though it's not perfect and cookies should ideally be grouped in one header. See e.g. http://kristol.org/cookie/errata.html#multi-header – Pekka Jul 28 '13 at 08:29
  • @Anh can you show some more code? Are the cookies present the next time you open the page? – Pekka Jul 28 '13 at 08:29
  • @Pekka웃 i will update the question – Anh Hậu Jul 28 '13 at 08:32
  • I guess you run into timing problem with ajax (because ajax is asyn). If you send the first ajax for login and send the second ajax right after that, the response of the first ajax (including Set-Cookie) may not arrive yet and the cookie is not set. – Khanh TO Jul 28 '13 at 08:32
  • @KhanhTO i updated the question, i think it is related to this link https://drupal.org/node/1133084 – Anh Hậu Jul 28 '13 at 08:39
  • Are you on `https://www.hafiz.gov.sa` when you are making this call? Remember, Ajax requests can't generally go outside their domain. Are you getting data back from the call? The Drupal bug you mention doesn't apply, it's about sending Cookie headers *from client side* – Pekka Jul 28 '13 at 08:43
  • @Pekka웃 it's remote server, i make a cross-domain ajax request. I think the Response Header will set cookie for domain "www.hafiz.gov.sa" but because of some policy, this action is not success – Anh Hậu Jul 28 '13 at 08:49
  • I don't think that is going to work in the form you show above. See [JQuery ajax cross domain](http://stackoverflow.com/q/3506208) – Pekka Jul 28 '13 at 08:55
  • @Pekka웃 i updated the question, you can run it, it work now – Anh Hậu Jul 28 '13 at 09:02
  • Really? That would seem weird, as you would still hit the cross-domain barrier. So it works for you now, problem solved? – Pekka Jul 28 '13 at 12:37
  • no sir, when i call the second ajax, because the server will check for this cookie so it fail – Anh Hậu Jul 29 '13 at 01:50

0 Answers0