Native Ajax and JQ Ajax

Source: Internet
Author: User

Preface:This section describes a small example of Using ajax to exchange data with the background. Therefore, the demo must be opened on the server. The server environment is very well established. Download Wamp or XAMPP from the Internet, install it step by step, and then place the prepared page in the specified location on the server. When opening, enter "localhost/specified page" or "127.0.0.1/specified page" in the address bar of the browser to open the page.

The following lists the demo's HTML, PHP, native Ajax, and JQ Ajax code.

 HTML code:

<! Doctype HTML> 

  PHP code:

<? PHP $ details = array ('spring' => " <p> April fangfei, the peach blossom in the temple is blooming. </P> ", 'summer '=>"  <p> the crystal curtain starts from the breeze, full rose one courtyard incense </P> ", 'fall' =>"  <p> Jinjing Wutong autumn leaf yellow, the curtain does not roll the night cream </P> ", 'Winter '=> " <p> Mei xueshen, snow but plum blossom </P> "); echo $ details [$ _ request ['liname'];?>

 Native Ajax:

<SCRIPT type = "text/JavaScript"> var Lis = document. getelementbyid ('LIST '). getelementsbytagname ('lil'); window. onload = initpage; function initpage () {for (VAR I = 0; I <Lis. length; I ++) {TXT = Lis [I]; TXT. onclick = function () {getdetails (this. ID) ;}} function creatrequest () {try {request = new XMLHttpRequest ();} catch (tryms) {try {request = new activexobject ("msxml2.xmlhttp ");} catch (OTH ERMS) {try {request = new activexobject ("miscrosoft. XMLHTTP ");} catch (failed) {request = NULL ;}} return request;} function getdetails (itemname) {request = creatrequest (); If (request = NULL) {alert ('failed to create request') return;} var url = "getdetails. PHP? Liname = "+ escape (itemname); Request. open ("get", URL, true); Request. onreadystatechange = displaydetails; request. send (null);} function displaydetails () {If (request. readystate = 4) {If (request. status = 200) {detaildiv = document. getelementbyid ("inf"); detaildiv. innerhtml = request. responsetext ;}}</SCRIPT>

  JQ Ajax:

<script type="text/javascript">
$ ('# List li'). Click (function () {$. Ajax ({type: 'get', data: '', URL:" getdetails. php? Liname = "+ this. ID, success: function (data) {response ('{inf'}.html (data) ;}, datatype: 'text', error: function () {alert (" failed! ");}})});
</script>

Native Ajax and JQ 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.