52,797
社区成员
发帖
与我相关
我的任务
分享
var str = xmlHttpGet.responseText;
str.search(/id="__viewstate"\svalue="([^"]+)/i);
//alert(RegExp.$1);
//console.log(RegExp.$1);
/*
e = document.createElement('div');
e.style.display="none";
e.innerHTML = xmlHttpGet.responseText;
document.body.insertBefore(e,null);
//alert(document.getElementsByName('__VIEWSTATE')[0].value);
__VIEWSTATE=document.getElementsByName('__VIEWSTATE')[0].value;
*/
__VIEWSTATE=RegExp.$1;
<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc()
{
var xmlHttpGet;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlHttpGet=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlHttpGet=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttpGet.onreadystatechange=function()
{
if (xmlHttpGet.readyState==4 && xmlHttpGet.status==200)
{
document.getElementById("myDiv").innerHTML=xmlHttpGet.responseText;
}
}
xmlHttpGet.open("GET","http://www.baidu.com/s?wd="+document.getElementById("txtValue").value,true);
xmlHttpGet.send();
}
</script>
</head>
<body>
<input id="txtValue" type="text" value="" />
<button type="button" onclick="loadXMLDoc()">Change Content</button>
<div id="myDiv"><h2>Let AJAX change this text</h2></div>
</body>
</html>
如果在非IE中执行,如我用Firefox 23,在FireBug可以看到状态码200,但是查看响应的信息,却是空白的。用抓包工具倒是可以看到:
接收: 结果代码:0x00000000
HTTP/1.1 200 OK
Date: Sat, 05 Oct 2013 06:31:36 GMT
Server: BWS/1.0
Content-Length: 17593
Content-Type: text/html;charset=utf-8
Cache-Control: private
BDPAGETYPE: 3
BDUSERID: 0
BDQID: 0xd764795a168fc903
Set-Cookie: BDSVRTM=97; path=/
Set-Cookie: H_PS_PSSID=3525_3379_1431; path=/; domain=.baidu.com
Set-Cookie: BAIDUID=CB49FD5DC8B81713492F21FC49440FE9:FG=1; expires=Sat, 05-Oct-43 06:31:35 GMT; path=/; domain=.baidu.com
Content-Encoding: gzip
P3P: CP=" OTI DSP COR IVA OUR IND COM "
Connection: Keep-Alive
于是,在XmlRequest对象中加上这一句:
xmlHttpGet.setRequestHeader("Access-Control-Allow-Origin","*");
在IE,正常请求,响应。
在FireFox23.
在FireBug中看到,直接被403禁止了。
抓包结果:
发送: 结果代码:0x00000000
OPTIONS /s?wd=ddddddddddd HTTP/1.1
Host: www.baidu.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Firefox/24.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Origin: null
Access-Control-Request-Method: GET
Access-Control-Request-Headers: access-control-allow-origin
Connection: keep-alive
响应的信息就根本看不到了,FireFox禁止了?