JS Basic AJAX Coding Sample Code

Source: Internet
Author: User
Tags http request

  This article mainly introduces JS Basic Ajax writing, the need for friends can refer to the following

The code is as follows: var xmlhttp = null;  function Myajax () { //1, create XMLHttpRequest object  //2, You need to write different code   if (Windows) in different ways for IE and other browsers to create this object. XMLHttpRequest) { //for ff,mozilar,opera,safari,ie7,ie8  xmlhttp = new XMLHttpRequest (); //fix some browsers bug& nbsp if (xmlhttp.overridemimetype) {  xmlhttp.overridemimetype ("Text/xml"); } } else if (window. ActiveXObject) { //IE6 below for browsers   var activexname = ["MSXML2. XMLHTTP "," Microsoft.XMLHTTP "," ""];  for (var i = 0; i < activexname.length; i++) {  Try { //Remove a control name creation, if the creation succeeds then stops, conversely throws the exception   XMLHTTP = new ActiveXObject (activexname[i));  break;& nbsp catch (e) {} } }   //need to confirm XMLHTTP creation was successful   if (!xmlhttp) {  alert ("XMLHttpRequest Create failed!") ;  return; } else {  alert (XMLHTTP); }   //Registration callback function. Note the registration callback function is not parenthesized and adds the value of the function back to onreadystatechange  Xmlhttp.onreadystatechange = callback; //Set Connection information  // The first parameter represents the HTTP request method, which supports all HTTP request methods, mainly makingThe URL address of the request is represented by a get and post //second parameter, and the parameter requested by the Get method also represents an asynchronous or synchronous interaction in the urlkh //third interface parameter, true for the asynchronous   Xmlhttp.open ("Get" , "servlet/checkusername?username=" + UserName, True); //Send data representations and server-side interaction  //sync, send this will be done after server-side data comes back   Xmlhttp.send (NULL);   //asynchronous mode, send this sentence completes the execution of  //post mode request code  //xmlhttp.open immediately ("POST", " Servlet/checkusername ", True"; //post method requires you to set HTTP request headers  //xmlhttp.setrequestheader ("Content-type", " Application/x-www-form-urlencoded "); //post mode send data  //xmlhttp.send (" username= "+username); }   //Callback functions   function callback () { //Judge the state of the object is interactive completion   if (xmlhttp.readystate = 4) { //To determine whether HTTP interaction is successful   if (xmlhttp.status =) { //Get server-side returned data  //Get server-side output of plain text data   var responsetext = Xmlhttp.responset ext;  alert (responsetext); } } }   
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.