Ask a question about Ajax

Source: Internet
Author: User
Ask an Ajax question
JScript Code
       function Nums () {var n = document.natform.num.value;        var url= "index.php?c=nat&a=natid&id=" +n+ "";            if (n = = ") {document.getElementById (' num '). style.display= ' block ';            document.getElementById (' num '). innerhtml= ' number cannot be empty ';            document.getElementById (' num '). style.color= ' Red ';        return false;            }else{XMLHttpRequest = Createxmlhttprequest ();            Xmlhttprequest.onreadystatechange = handle;            Xmlhttprequest.open ("GET", url,true);                        Xmlhttprequest.send (NULL); My question is, in this else, how do I receive the return value of handle () (True False)}} function handle () {if (Xmlhttprequest.readysta            Te = = 4) {var re = Xmlhttprequest.responsetext;                if (re) {document.getElementById (' num '). style.display= ' block ';                document.getElementById (' num '). innerhtml= ' number already exists '; document.getElementById (' num '). style.color= ' Red ';            return false;                }else{document.getElementById (' num '). style.display= ' block ';                document.getElementById (' num '). Innerhtml= ' can be used ';                document.getElementById (' num '). style.color= ' green ';            return true; }                    }        }


The above is an AJAX judgment ID whether it exists, if not present, can be committed, if there is a hint exists, cannot commit
JScript Code
  
       function saves () {        if (Nums ()) {            document.natform.action= "index.php?c=nat&a=addnatpost";                    Document.natform.submit ();        }    }

But now the problem is that num () does not receive the return value of handle (), so the NUM () state is undefined when it exists, so ask how to change it.

------Solution--------------------
JScript Code
var re = xmlhttprequest.responsetext;    What is the value of re here? Alert look. If there is no value, the problem is that the backend receives processing
------Solution--------------------
Ajax is an asynchronous way of communicating, so you can't get the correct return value from num ()
The commit action you need should be done in handle
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.