javascript - AJAX request status returns 0 - Stack Overflow

when i make a AJAX request with this code, it returns the status as 0. what did i do wrong? Also, this

when i make a AJAX request with this code, it returns the status as 0. what did i do wrong? Also, this code is only designed to work in Firefox for various reasons.

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);
}
}
}

when i make a AJAX request with this code, it returns the status as 0. what did i do wrong? Also, this code is only designed to work in Firefox for various reasons.

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);
}
}
}
Share Improve this question asked Nov 27, 2009 at 18:58 John StimacJohn Stimac 5,4918 gold badges43 silver badges60 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

If you try to connect to another server, the same origin policy will stop you:

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.

jquery. You can only connect to another server if you use JSONP and a callback. The other server will need to support the JSONP/callback mechanism. This method involves creating a new script tag then having the remote server return the callback code into this script tag. Look at the jQuery source code (or simply use it) to see it handles JSONP.

The basic idea is to create a script tag with the source as the remote URL. The remote method returns a "script" containing a function call using the callback and the resultant JSON data.

I've recently run into this issue. I've been using AJAX to submit form results so that page elements can update automatically. My domain calls were correct. SELECT statements on the database would work, but INSERT, UPDATE, and DELETE statements would not.

Alerts showing progress through the javascript indicated that the call was running correctly, but it would never make it to the php portion of the process. The simple answer was that the form's onsubmit element needed to include return false;. After adding that, the things ran smoothly.

<form name="form" action="" method="post" onsubmit="ProcessRequest(this);return false;">

Hope this helps.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744245887a4564926.html

相关推荐

  • javascript - AJAX request status returns 0 - Stack Overflow

    when i make a AJAX request with this code, it returns the status as 0. what did i do wrong? Also, this

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信