当我使用此代码发出AJAX请求时,它返回状态为0.我做错了什么?另外,出于各种原因,此代码仅设计用于Firefox.
var ajax;
function connectToOtherServer(server,port,userid,password){
ajax=new XMLHttpRequest();
ajax.onreadystatechange=validateConnection;
params='userid='+encodeURIComponent(userid)+'&password='+encodeURIComponent(password);
alert('http://'+server+':'+port+'/ok.txt');
ajax.open('POST','http://'+server+':'+port+'/ok.txt',true);
ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded");
ajax.setRequestHeader("Content-length",params.length);
ajax.setRequestHeader("Connection","close");
ajax.send(params);
}
function validateConnection(){
if(ajax.readyState===4){
if(ajax.status===200){
alert(ajax.responseText);
}else{
alert(ajax.status);
}
}
}
解决方法:
如果您尝试连接到另一台服务器,则same origin policy将阻止您:
The term “origin” is defined using the
domain name, application layer
protocol, and (in most browsers) TCP
port of the HTML document running the
script. Two resources are considered
to be of the same origin if and only
if all these values are exactly the
same.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。