Cookie Access over JSONP

后端 未结 2 832
情书的邮戳
情书的邮戳 2021-01-02 10:24

I have a page in domain.com that makes a JSONP ajax request (using jQuery\'s .getJSON() function) to a URL in anotherdomain.com. I tho

相关标签:
2条回答
  • 2021-01-02 11:00

    I have encountered the same problem before. The issue I found is that most browsers won't let you ESTABLISH a session (i.e. set a session cookie) when the same origin policy isn't being met.

    0 讨论(0)
  • 2021-01-02 11:06

    The same origin policy applies to all ajax requests, so if the domain being accessed in an ajax call is different than the domain loaded in the browser (document.host), all cookies associated with the domain in the requested url will not be sent up. Therefore, the JSONP approach works because it writes out a new script tag in the window, which will behave like any resource request a browser could make to an external domain (hence passing all the cookies associated with the domain in the url). I have also confirmed this by simply calling $.post("http://atdmt.com") from my chrome console, while on stackoverflow.com in the browser (the only other domain that had cookies in my browser, while writing up the answer) and it did not send up any cookies in the request headers.

    Another solution to get around the problem of maintaining state for anotherdomain.com would be to have anotherdomain.com set a first party cookie (by not setting the domain attribute of the cookie) and when an ajax/json request is made to anotherdomain.com access those cookies via javascript and push them up the request using standard HTTP params.

    Hope I have helped.

    0 讨论(0)
提交回复
热议问题