问题
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