Make multiple AJAX request using the same session

和自甴很熟 提交于 2020-02-05 06:36:15

问题


I have a REST webservice that returns an hash value based on the current http session. If I open the webservice page using a browser, I will see the same value refreshing the page or opening the page in multiple tab. This is the expected behavior because I'm using the same session on multiple request. If I do an AJAX request using the $http service of AngularJS, I obtain a different value each time. It seems that each request uses a different session. I need to obtain the same behavior like the browser request, multiple request that sharing the same session. Is it possible?


More info about server environment:

The server side REST webservice is powered by Laravel 4.2, there is a simple RESTful controller that return the hash code using this function:

public function getCsrf () {
    return Response::json(array('csrf' => csrf_token()));
}

if I browse the webservice page using my browser I obtain always the same result (i.e. http://myservice.page/rest/csrf), If I do the same thing using ajax I obtain always different results.


回答1:


I would do it on the server side: cache the hash value. If an ajax request comes in, I would determine if it is already associated with a map of session/hash value. return it if it exists.

You can also put the hash value in sessionSorage, and do some checking logic in angular httpInterceptor, but this is more fragile than backend



来源:https://stackoverflow.com/questions/25624370/make-multiple-ajax-request-using-the-same-session

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!