简体   繁体   中英

Ajax ready state : 0 always in javascript

This the part of my code where I create the request. ( I do not want to use jquery)

var xr = new XMLHttpRequest();
console.log(xr);
xr.onreadystatechange = function () {
    console.log(xr.readyState,xr.status);
    if(xr.readyState == 4 && xr.status == 200) {
        var data=xr.responseText;
        alert(data.innerHTML);
        console.log(data);
    }
    var params ="param1="+movieName+"&param2"+movieGenre ;
    xr.open("GET","./saveMovie.php"+"?"+params,true);
    xr.send();
}

You need to open and sed the XHR outside the callback function.

var xr = new XMLHttpRequest();
console.log(xr);
xr.onreadystatechange = function () {
    console.log(xr.readyState,xr.status);
    if(xr.readyState == 4 && xr.status == 200) {
        var data=xr.responseText;
        alert(data.innerHTML);
        console.log(data);
    }
}
var params ="param1="+movieName+"&param2"+movieGenre ;
xr.open("GET","./saveMovie.php"+"?"+params,true);
xr.send();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM