Two ways to implement Ajax

Source: Internet
Author: User

Let's start with the usual jquery-style Ajax notation, and the value in Java backend

1 /**2 *ajax3  */4 functionShowlasttime () {5     varFacilityId = $ (' *[name= "facilityId"] "). Val ();//Page Values6     varMaintetype = $ ("#mainteType"). Val (); 7     varMAINTEWORKNM = $ ("#mainteWorkNm"). Val (); 8     varurl = ap_base_url + "c412s/showlastymd/";//Generation of URLs9     if(FacilityId! = "" &&maintetype! = "" &&mainteworknm! = ""){Ten $.ajax ({ OneAsyncfalse,//false is synchronous (the user needs to wait for the refresh to be done), true to be asynchronous (no wait required, other operations can be done) AUrl:url,//the requested URL -Type: ' POST ',//two ways post and get,post are invisible, get will spell the parameters and values in the URL (recommended post) - data: { the                 //The following parameters are required to be passed -' FacilityId ': FacilityId, -' Maintetypecd ': Maintetype, -' Mainteworkid ': MAINTEWORKNM +             }, -             //Cache:false, +Errorfunction(XMLHttpRequest, Textstatus, Errorthrown) { A Ajaxerror (XMLHttpRequest, Textstatus, errorthrown); at             }, -Successfunction(data) {//data returned back to the background -$ (". Info"). HTML (data);//What to do after receiving the postback data -             } -         }); -}Else{ in$ (". Info"). Text (""); -     } to     return false; +}

The second is the common JS of the AJAX wording

functionshowLastTime2 () {varMaintetype = $ ("#mainteType"). Val (); varMAINTEWORKNM = $ ("#mainteWorkNm"). Val (); varFacilityId = $ (' *[name= "facilityId"]). Val (); varurl = ap_base_url + "c412s/showlastymd2/"; varXMLHTTP; //Create a XMLHttpRequest object    if(Window. XMLHttpRequest) {//code for ie7+, Firefox, Chrome, Opera, Safarixmlhttp=NewXMLHttpRequest (); }Else{//code for IE6, IE5xmlhttp=NewActiveXObject ("Microsoft.XMLHTTP"); }    if(FacilityId! = "" &&maintetype! = "" &&mainteworknm! = "") {Xmlhttp.open ("POST", URL,false);//send a request to the server, False for synchronization (the user needs to wait for the refresh to be done), true to be asynchronous (no waiting, other operations are possible)Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded");//If you must add the line code by using the Post method, add the HTTP header to the requestXmlhttp.send ("maintetypecd=" +maintetype+ "&mainteworkid=" +mainteworknm+ "&facilityid=" +facilityId);//Pass the referenceXmlhttp.onreadystatechange=function(){//trigger Event If server status changes            if(xmlhttp.readystate==4 && xmlhttp.status==200) {//do not understand the following table, to the effect that when the request is complete and the status is OK$ (". Info"). HTML (xmlhttp.responsetext);//Xmlhttp.responsetext values returned for the server            }        }; }Else{        $(". Info"). Text (""); }

Properties Description
onReadyStateChange The function (or function name) is called whenever the ReadyState property is changed.
ReadyState

The state of being xmlhttprequest. Vary from 0 to 4.

  • 0: Request not initialized
  • 1: Server Connection established
  • 2: Request received
  • 3: In Request processing
  • 4: The request is complete and the response is ready
Status

$: "OK"

404: Page Not Found

Background Java code value

1  Public String showLastYmd2 (httpservletrequest request HttpServletResponse response) {2     Request.getparameter ("FacilityId");3     request.getparameter ("MAINTETYPECD");4      request.getparameter ("Mainteworkid");5     Response.Write ("Data")    ; // Pass back Parameters 6 }

Two ways to implement Ajax

Related Article

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.