Novice Learning Ajax2

Source: Internet
Author: User

Today, the main solution to a two-day problem of Ajax, is about to get data from the server synchronization and asynchronous problems, Xhr.open ("Get", url,false); Here are three parameter "get" means to get the way, in addition to get and Post,head, The second is the address this address is the server you want to get the address of the file, the file can be XML or TXT, please note here is the server address, corresponding to the directory under Tomcat, for example: Var url=http://localhost:8080/ Wxpay/code.txt refers to the WebApp under the Tomcat under the Wxpay under the Code.txt content, so you want to put the server's content in the corresponding directory can be obtained by the client, the last parameter is false this is what I want to say today, It is this put me into the pit, I was set it to true, originally did not know what this means, and then the program always appeared a problem is that the return value of the JS function is empty, but if I add an alert output in front of the returned variable then the return value is normal but the alert is empty, At that time, feel magical and dazed, and then through their own debugging to the problem to the server interaction with the problem, and then hold the attitude to try to change the true FALSE,TMD success, normal, as a good programmer must have to have the spirit of the inquisitive ah, no matter how the principle ah, Later holding a pleasant mood to check the information, to be synchronous and asynchronous problem, synchronous simple understanding is a walk, you do not go LZ, I wait for you, and asynchronous it, is can not walk, you do not go, pull, I go first, so it can be said, and server interaction is divided into should be four steps, Synchronous is false when you get the data to go down, asynchronous words server interaction every step can be executed, so it is possible to do not obtain the server data has been executed down the return of the results of course is not what we want.

Read the story, look at the code,

varXhr=false;
varoutmsg="";
function MakeRequest () {
if(Window. XMLHttpRequest) {//The object exists that indicates that the current browser is one of the firefox,opera,safiar,ie7.0,ie8.0 that has this object
Xhr=NewXMLHttpRequest (); }
Else{
if(Window. ActiveXObject) {//the object is present in the ie6.0,5.5

Try{
Xhr=NewActiveXObject ("Microsoft. XMLHTTP");
}
Catch(e) {}
}
}
//alert (XHR);
if(XHR) {//If you get the XML object
//alert ("GETXHR");
xhr.onreadystatechange=showcontents;
varUrl="Http://localhost:8080/wxPay/code.txt";
Xhr.open ("GET"Urlfalse);//The address request of the requested method (get post Head) file is asynchronous i.e. whether we wait until the request is complete
Xhr.send (NULL);
}

returnoutmsg;
}
function showcontents () {
//alert (xhr.readystate+ "state");
if(xhr.readystate==4){//response has been fully accepted
//alert (xhr.status);
if(xhr.status== $){//200 indicates that the requested file exists 404 indicates that the file does not exist
Outmsg=xhr.responsetext;
}

//return outmsg;
}
//alert (outmsg+ "outmsg");
}

Novice Learning Ajax2

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.