日期:2014-05-16  浏览次数:20833 次

Chrome下xmlHttpRequest的open方法报DOMException
本帖最后由 showbo 于 2012-10-11 14:30:34 编辑
RT!我贴出我的代码,在IE下各种访问毫无压力.Chrome 版本 22.0.1229.94 m.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Login</title>
</head>
<script>
function createNewRequest(){
var HttpRequest = null ;
try {
//if (window.XMLHttpRequest) { // Mozilla, Safari, ...
HttpRequest = new XMLHttpRequest();
//} else if (window.ActiveXObject) { // IE
//HttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
//}
if (HttpRequest.overrideMimeType) {
// set type accordingly to anticipated content type
if(type.toUpperCase() == "XML")
HttpRequest.overrideMimeType('text/xml');
else
HttpRequest.overrideMimeType('text/html');
}
} catch (e) {
//showWarnningMessage(errorText, e.message);
}
return HttpRequest ;
}
function ajaxCall(urlString,data){
var pageRequest = createNewRequest();
pageRequest.open('GET', urlString);
//pageRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
pageRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
var encodedData = encodeURI(data);
//pageRequest.send("");
pageRequest.send(encodedData);
pageRequest.onreadystatechange=function(){
//alert("readyState = " + pageRequest.readyState + "; HTTP Status = " + pageRequest.status);
//var returnText = pageRequest.responseText ;
if (pageRequest.readyState == 4 && pageRequest.status == 200){
document.getElementById("f1").src="http://172.16.5.158:9080/portal/jsp/viewScoreboard.do?id=2015.209";
}
}
}
function doSubmit(){
ajaxCall("http://172.16.5.158:9080/portal/j_security_check?j_username=admin&j_password=admin","abcd");
}
</script>
<body>
<input type="button" value="Report" onclick="doSubmit()">
<iframe style="display:inline" src="" id="f1" name="f1" width=100% height=100%></iframe>
</body> 
</html>

------解决方案--------------------
pageRequest.open('GET', urlString);
改成
pageRequest.open('post', urlString);

get方法无需
pageRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");

pageRequest.send(encodedData);
这些

另外注意不要跨域
------解决方案--------------------
var pageRequest = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
pageRequest.open('post', urlString);
pageRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
pageRequest.setRequestHeader('Content-Length',data.length);
pageRequest.setRequestHeader("Connection","close");
pageRequest.send(data);
pageRequest.onreadystatechange=function(){
if (pageRequest.readyState == 4 && pageRequest.status == 200){
document.getElementById("f1").src="http://172.16.5.158:9080/portal/jsp/viewScoreboard.do?id=2015.209";
}
}