在javascript中读取请求标头

时间:2013-08-21 09:28:01

标签: javascript jquery-mobile

是否可以读取请求标头,然后在jquery mobile + javascript代码中获取变量值(例如用户ID)?如果是的话有什么例子

我找到了这篇文章。 How to get read data from response header in jquery/javascript,但不确定它是如何运作的。

3 个答案:

答案 0 :(得分:3)

使用它:

getAllResponseHeaders() 

更多细节 here

或者这个:

getResponseHeader()

更多细节 here

答案 1 :(得分:1)

 $.ajax({
 type: 'POST',
 url:'url', //the request url
 data: {}, //any param that you want to send in the request
 success: function(data){ //'data' is the response
    alert(request.getResponseHeader());
 }
 error: function () {
    alert("failed");
 }
 });

答案 2 :(得分:0)

如果你想要这个页面的请求标题,你可以通过php getallheaders获取它们并将它们显示为javascript数组。

<script>
var headers = <?php echo json_encode(getallheaders()); ?>;
</script>