AJAX Request Status Return 0

when I make an AJAX request with this code, it returns the status as 0. What did I do wrong? In addition, this code is intended only for use 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);
}
}
}
+3
source share
3 answers

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) the TCP port of the HTML document in which the script is executed. Two resources are considered to be of the same origin, if and only if all these values ​​correspond to the same.

+3

jquery.com , JSONP . JSONP/callback. script, script. jQuery ( ), , JSONP.

script URL-. "script", , JSON.

+1

. AJAX , . . SELECT , INSERT, UPDATE DELETE .

, javascript, , , php- . , onsubmit return false;. , .

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

, .

0

Source: https://habr.com/ru/post/1724137/


All Articles