请使用同一个会话的多个AJAX请求 [英] Make multiple AJAX request using the same session
问题描述
我有一个REST Web服务返回基于当前HTTP会话的哈希值。如果我使用的是浏览器中打开该web页面,我会看到相同的值刷新页面,或在多个选项卡中打开该网页。这是因为我使用的是同一个会话的多个请求的预期行为。如果我使用AngularJS的$ HTTP服务处理Ajax请求,我得到一个不同的值,每次。似乎每个请求使用不同的会话。我需要得到像浏览器请求,多个请求共享同一个会话相同的行为。可能吗?
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:
,服务器侧的REST web服务由Laravel 4.2供电,有一个简单的 RESTful的控制器一>在使用此功能返回的哈希code:
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()));
}
如果我用我的浏览器浏览web服务页面我总是得到相同的结果(即 http://myservice.page / REST / CSRF ),如果我用ajax做同样的事情,我总是得到不同的结果。
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.
推荐答案
我会做在服务器端:缓存的哈希值。如果一个Ajax请求时,我会确定其是否已与会话/散列值的地图相关联。如果存在返回。
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.
您也可以把sessionSorage的散列值,并做角httpInterceptor一些检查逻辑,但是这比后端更加脆弱
You can also put the hash value in sessionSorage, and do some checking logic in angular httpInterceptor, but this is more fragile than backend
这篇关于请使用同一个会话的多个AJAX请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!