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

Chrome下xmlHttpRequest的open方法报DOMException
RT!我贴出我的代码,在IE下各种访问毫无压力.Chrome 版本 22.0.1229.94 m.

HTML code
<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";
}
}
------解决方案--------------------
再次提醒,你访问的方法必须是http://172.16.5.158:9080/xxx.htm
不能跨域 不能跨端口
------解决方案--------------------
静态页面也是通过http://172.16.5.152:9080/页面打开的ajax请求http://172.16.5.152:9080/上的资源不算,

但是请求
http://172.16.5.158:9080/【ip地址不一样】
http://172.16.5.152:8008/【端口不一样】

这些的算
------解决方案--------------------
探讨

我在我本地的静态页面上请求172.16.5.152上的东西算不算跨域?因为在这里Open始终失败.