简体   繁体   中英

AJAX ready state 4 and status 0

I am trying to make an AJAX call (pure JavaScript) for storing an user in DB.

My java script file contains the following code:

var url="interfata_db.php";
    xmlhttp.onreadystatechange=function(){
        alert('ready state ' + xmlhttp.readyState + ' status ' + xmlhttp.status);
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            alert(xmlhttp.responseText);
        }
    };   
    xmlhttp.open("POST",url,true); 
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp.send("tag=" + tag + "&name=" + nume + "&first_name=" + prenume + "&email=" + email + "&password=" + parola1);

In interfata_db.php I set the header:

header('Content-type: application/json');

and I built the response that is returned as JSON:

echo json_encode($response);

The problem is that when I make the AJAX call the status is 0 for readyState = 4. In Chrome Developer Tool interfata_db.php appears to be cancelled. Chrome开发者工具

I want to mention also that the user is stored successfully if it doesn't exist already.

What should I do to get the status 200?

Thanks!

You are not canceling the action that is causing the page to navigate away. You need to stop it with preventDefault() or returning false to stop the default action.

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