Ajax processing server returns three data type methods _ajax related

Source: Internet
Author: User
Tags eval tag name tagname

The principle is simple, the structure is basically unchanged, just change the way of processing the returned data.

1.text/html format
this type of return processing is simple, and is used directly as a string. For ease of use, encapsulate the following functions:

/**
 * @function use AJAX dynamic Exchange data (text/html format)
 * @param URL  to submit the requested page
 * @param jsondata the data to be submitted, using JSON delivery
 * @param getmsg This function can get to the processed data/
function Ajaxtext (url,jsondata,getmsg)
{
  ///Create Ajax objects, ActiveXObject compatible ie5,6
  var oajax = window. Xmlhttprequest?new XMLHttpRequest (): New ActiveXObject ("Microsoft.XMLHTTP");
  Open Request
  oajax.open (' POST ', url,true);//method, URL, asynchronous transmit
  //Send request
  var data = ';
  for (var d in Jsondata)  //Assembly Data
    + + (d + ' = ' +jsondata[d]+ ' & ');
  Oajax.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
  Oajax.send (data);
  Receive return, triggering
  Oajax.onreadystatechange = function ()
  {
    if (oajax.readystate = 4 &&) When something is returned from the server Oajax.status = =/
    {
      if (getmsg) getmsg (Oajax.responsetext);
    }
  }
}

The server-side return data format is as follows:
For example:

Returns the XML format
//header ("Content-type:text/xml;charset=utf-8");
Returns the text or JSON format
header ("Content-type:text/html;charset=utf-8");
Disabling caching is a normal submission for data, rather than caching the data
header ("Cache-control:no-cache");
$username = $_post[' username ']; Get user name
if (empty ($username))
  echo ' Please enter username ';
else if ($username = = ' Acme ')
  Echo ' username has been registered ';
else
  Echo ' username available ';

The calling format is as follows:

url = ' abc.php ';
var jsondata={username: ' Acme ', PASSW: ' Acme '};
Ajaxtext (url,jsondata,getmsg);
function getmsg (msg)
{
 //do something
}

2.XML format

Returns an XML DOM object that parses data similar to HTML DOM programming. For example, get the Label object (array form) by name, get the desired label object from the array, and then get the text value from the Label object.
The functions are as follows:

/** * @function use AJAX dynamic Exchange data (XML format) * @param URL to submit the requested page * @param jsondata the data to be submitted, using JSON pass * @param tagName to get the value of the tag name * @param getmsg This function can get to the processed data/function Ajaxxml (url,jsondata,tagname,getmsg) {/ /Create Ajax object, ActiveXObject compatible ie5,6 var oajax = window.
  Xmlhttprequest?new XMLHttpRequest (): New ActiveXObject ("Microsoft.XMLHTTP");
  Open Request Oajax.open (' POST ', url,true);//method, URL, asynchronous transmit//Send request var data = ';
  for (var d in jsondata)//Assembled data + + (d + ' = ' +jsondata[d] + ' & ');
  Oajax.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
  Oajax.send (data); Receive return, triggering oajax.onreadystatechange = function () {if (oajax.readystate = = 4 && oajax.status = 2) When something is returned from the server
      {var oxml = Oajax.responsexml;//Returns an XML DOM object var Otag = Oxml.getelementsbytagname (tagName);
      var tagvalue = Otag[0].childnodes[0].nodevalue;
    if (getmsg) getmsg (Tagvalue); }
  }
}

The server-side return data format is as follows:
For example:

Returns the XML-formatted
header ("Content-type:text/xml;charset=utf-8");
Returns the text or JSON format
//header ("Content-type:text/html;charset=utf-8");
Disabling caching is a normal submission for data, rather than caching the data
header ("Cache-control:no-cache");
$username = $_post[' username ']; Get user name
if (empty ($username))
  echo ' <user><mes> Enter user name </mes></user> ';//tags can be customized
else if ($username = = ' Acme ')
  Echo ' <user><mes> username has been registered </mes></user> ';
else
  Echo ' <user><mes> username available </mes></user> ';

The calling format is as follows:

var url = ' abc.php ';
var Jsondata = {username: ' Acme '};
Ajaxxml (url,jsondata, ' mes ', getmsg);
function getmsg (msg)
 {
   //do something
 }

3. Return JSON

The functions are as follows:

/**
 * @function uses AJAX to dynamically exchange data (text/html format), but returns the JSON type of text data
 * @param URL  to submit the requested page
 * @param jsondata Data to be submitted, using JSON to pass
 * @param getmsg This function can get to the processed data/function
Ajaxjson (url,jsondata,getmsg)
{
  // Create Ajax objects, ActiveXObject compatible ie5,6
  var oajax = window. Xmlhttprequest?new XMLHttpRequest (): New ActiveXObject ("Microsoft.XMLHTTP");
  Open Request
  oajax.open (' POST ', url,true);//method, URL, asynchronous transmit
  //Send request
  var data = ';
  for (var d in Jsondata)  //Assembled data
    + + (d + ' = ' +jsondata[d] + ' & ');
  Oajax.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
  Oajax.send (data);
  Receive return, triggering
  Oajax.onreadystatechange = function ()
  {
    if (oajax.readystate = 4 &&) When something is returned from the server Oajax.status = =
    {
      var json = eval (' (' +oajax.responsetext+ '));//Parse the returned string into a JSON object
      if (getmsg) Getmsg (JSON);}}


The server-side return data format is as follows:

For example:

Returns the XML format
//header ("Content-type:text/xml;charset=utf-8");
Returns the text or JSON format
header ("Content-type:text/html;charset=utf-8");
Disabling caching is a normal submission for data, rather than caching the data
header ("Cache-control:no-cache");
$username = $_post[' username ']; Get user name
if (empty ($username))
  echo ' {mes ': "Please enter username"};
else if ($username = = ' Acme ')
  echo ' {mes ': ' username has been registered '} ';
else
  echo ' {mes ': ' User name available '} ';

The calling format is as follows:

url = ' abc.php ';
var jsondata={username: ' Acme ', PASSW: ' Acme '};
Ajaxtext (url,jsondata,getmsg);
function getmsg (msg)
{
 //do something
}

For ease of use, you can combine three functions. The merged functions are as follows:

/** * @function use AJAX to dynamically exchange data * @param URL to submit the requested page * @param jsondata the data to be submitted, using JSON to pass the * @param getmsg this function can get the processed data * @param type of data accepted, Text/xml/json * @param tagName type = XML This parameter is set to the label name of the text to get * @return no/function ajax (Ur L,jsondata,getmsg,type,tagname) {//Create Ajax object, ActiveXObject compatible ie5,6 var oajax = window.
  Xmlhttprequest?new XMLHttpRequest (): New ActiveXObject ("Microsoft.XMLHTTP");
  Open Request Oajax.open (' POST ', url,true);//method, URL, asynchronous transmit//Send request var data = ';
  for (var d in Jsondata)//assembly data + + (d + ' = ' +jsondata[d]+ ' & ');
  Oajax.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
  Oajax.send (data); Receive return, triggering oajax.onreadystatechange = function () {if (oajax.readystate = = 4 && oajax.status = 2) When something is returned from the server
      {if (type = = ' text ') {if (getmsg) getmsg (Oajax.responsetext);
     else if (type = = ' json ') {var json = eval (' (' +oajax.responsetext+ ') ');//Parse the returned string into a JSON object   if (getmsg) getmsg (JSON); else if (type = = ' xml ') {var oxml = Oajax.responsexml;//Returns an XML DOM object var Otag = Oxml.get
        Elementsbytagname (TagName);
        var tagvalue = Otag[0].childnodes[0].nodevalue;
      if (getmsg) getmsg (Tagvalue);
 }

    }
  }
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.