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