javascript/jquery读取和修改HTTP headers
2016-10-28 11:49
218 查看
javascript/jquery读取和修改HTTP headers
注意::W3规定XMLHttpRequest并不能修改全部的HTTP Headers,而仅是一小部分。
test.php:
jQuery通过XMLHttpRequest的
参考:
http://stackoverflow.com/questions/1145588/cannot-properly-set-the-accept-http-header-with-jquery
http://stackoverflow.com/questions/2444489/getresponseheader-is-not-a-function
2011/07/19 -- Cookies相关操作
2011/05/10 -- jQuery Ajax使用范例和JSON应用
2011/06/30 -- libcurl库及其使用实例
2011/06/30 -- C解析HTTP request和response
2011/06/29 -- C实现HTTP client
2011/05/10 -- JavaScript和PHP中json object和json string互相转换
jquery修改HTTP headers
jQuery Ajax可以通过headers或
beforeSend修改request的HTTP headers,例如:
$.ajax({ url: "./test.php", type: "POST", headers: { "Accept" : "text/plain; charset=utf-8", "Content-Type": "text/plain; charset=utf-8" }, /* beforeSend: function(jqXHR, settings) { jqXHR.setRequestHeader('Accept', 'text/plain; charset=utf-8'); jqXHR.setRequestHeader('Content-Type', 'text/plain; charset=utf-8'); }, */ data: {"user" : "min", "pass" : "he"}, error: function(jqXHR, textStatus, errorThrown) { //.... }, success: function(data, textStatus, jqXHR) { //.... } }
注意::W3规定XMLHttpRequest并不能修改全部的HTTP Headers,而仅是一小部分。
jquery获取HTTP headers:
test.html:<html> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script> </head> <body></body> <script type="text/javascript"> $(document).ready(function(){ $.ajax({ url: "./test.php", type: "POST", data: {"user" : "min", "pass" : "he"}, error: function(jqXHR, textStatus, errorThrown) { if (textStatus == "error") { alert(textStatus + " : " +errorThrown); } else { alert(textStatus); } }, success: function(data, textStatus, jqXHR) { alert(jqXHR.getResponseHeader("Server")); alert(jqXHR.getResponseHeader("Content-Type")); alert(jqXHR.getResponseHeader("X-Powered-By")); alert(jqXHR.getResponseHeader("Content-Encoding")); alert(jqXHR.getAllResponseHeaders()); alert(jqXHR.getResponseHeader("Set-Cookie")); //返回null,不能获取Set-Cookie的值 alert(data + textStatus); } }); }); </script> </html>
test.php:
<?php if (isset($_COOKIE["time"])) { $time = $_COOKIE["time"] + 1; } else { $time = 1; } setcookie("time", $time); $user = $_REQUEST["user"]; $pass = $_REQUEST["pass"]; print $user.$pass." ".$time;
jQuery通过XMLHttpRequest的
getResponseHeader或
getAllResponseHeaders()可以获取指定的HTTP header field的值,但规定不能获取
Set-Cookie和
Set-Cookie2的值。
参考:
http://stackoverflow.com/questions/1145588/cannot-properly-set-the-accept-http-header-with-jquery
http://stackoverflow.com/questions/2444489/getresponseheader-is-not-a-function
Related Posts
2011/07/19 -- JSONP实现Ajax跨域cross-domain访问2011/07/19 -- Cookies相关操作
2011/05/10 -- jQuery Ajax使用范例和JSON应用
2011/06/30 -- libcurl库及其使用实例
2011/06/30 -- C解析HTTP request和response
2011/06/29 -- C实现HTTP client
2011/05/10 -- JavaScript和PHP中json object和json string互相转换
相关文章推荐
- 修改HttpCustomHeaders 兼容IE8
- 修改HTTP消息头(例子)Modify HTTP Headers (Examples)
- 推荐一个IE小工具:ieHTTPheaders ----web开发、调试的好帮手
- 修改TTF字体美化显示效果【原文转自:http://www.linuxfans.org/】
- DotNetNuke与MemberShip的结合(五年版)第三步修改HttpModule.DNNMembership
- 修改TOMCAT/JBOSS的HTTPPORT
- 动网 登录出错 已将 HTTP 头输出到客户端浏览器。任何对 HTTP 头的修改都必须在输出页内容之前进行。
- 控件包含代码块(即 ),因此无法修改控件集合 (转自http://blog.csdn.net/wangchao1982/archive/2007/11/19/1892472.aspx)
- 修改prototype.js以实现xmlhttp的http-basic认证
- ieHttpHeaders
- Exchange 2003 OWA中修改密码[from http://www.msexchange.org]
- 在oracle 10g AS中,修改端口7777到80 (包含了 OC4J,默认情况下,是不是 J2EE 应用和 Http_Server)
- 修改http头,实现下载与查看
- HTTP Response Headers
- 推荐ieHTTPHeaders和单点登陆Single Sign-On
- 修改oracle 9i http服务的端口
- 怎样在VC++中访问和修改注册表(转载http://blog.csdn.net/amh/)
- What is ieHTTPHeaders?
- 修改apache和php的http头版本信息的方法
- The specified CGI application misbehaved by not returning a complete set of HTTP headers错误解决办法