XMLHtpRequest onreadystatechange



  • Hello, everyone.

    There's been a problem with a request for a server... ♪

    I don't understand why I'm in a condition. readyState || status ?

    What's going on in this code with onreadystatechange ?

    About Access control allow origin We need to build a respons in the code server,

    Is there any problem with this? onreadystatechange ?

    function registerHandler(user){
    

    var xhr = new XMLHttpRequest();

    xhr.onreadystatechange = function(){

    if(xhr.readyState!=4 || xhr.status!=200){
      //Error in Ajax request Why ?
    
            return alert("Problems with request");
    }
    
    }
    
        console.log("After onreadystatechange");
        xhr.open("POST","http://localhost:8080/CouponOnWeb/webappl/register",true);
        xhr.setRequestHeader('Content-type','application/json;charset=utf-8');
        xhr.send(user);  
    

    }

    function registerController(){

    var user = {
    
    username:document.getElementById("reg_user_name").value,
    firstname:document.getElementById("first_name").value,
    lastname:document.getElementById("last_name").value,
    userid:document.getElementById("user_id").value,
    password:document.getElementById("reg_password").value,
    email:document.getElementById("reg_email").value
    
    }
    

    registerHandler(user);

    }

    <form id = "registrator" onsubmit = "registerController()">

    <label for = "reg_user_name">User name:</label>
    <input id = "reg_user_name" name = "reg_user_name" type = "text" placeholder = "user name">
    <br>
    <label for = "first_name">First name:</label>
    <input id = "first_name" name = "first_name" type = "text" placeholder = "first name">
    <br>
    <label for = "last_name">Last name:</label>
    <input id = "last_name" name = "last_name" type = "text" placeholder = "last name">
    <br>
    <label for = "user_id">Id number:</label>
    <input id = "user_id" name = "user_id" type = "number" placeholder = "id number">
    <br>
    <label for = "reg_password">Password:</label>
    <input id = "reg_password" name = "reg_password" type = "password" placeholder = "password">
    <br>
    <label for = "reg_email">Email:</label>
    <input id = "reg_email" name = "reg_email" type = "email" placeholder = "email">
    <br>
    <input id = "reg_button" name = "reg_button" type = "submit" value = "Register">

    </form>

    введите сюда описание изображения

    введите сюда описание изображения



  • Right?

    function registerHandler(user){
        var xhr = new XMLHttpRequest();
        xhr.addEventListener('readystatechange', function () {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    // всё норм
                } else {    
                        alert("Problems with request");
                }    
            }    
        });    
        xhr.open('POST','http://localhost:8080/CouponOnWeb/webappl/register',true);
        xhr.send(user);  
    }
    

    and on the server in case of:

    header('Access-Control-Allow-Origin: *');  
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2