Ajax usage, implementation method, JS native and JQ implementations

Source: Internet
Author: User

AJAX explanations

Ajax is the implementation of page asynchronous loading.

It is often used to interact with the front and back end data to achieve no refresh change operation.

A must-have skill for web front-end and back-end user Development ~ ~

Ajax Operations ~:

The slang principle: in the slang, is to transfer the data in the past, and then get back to the data from the back to use

(JS native uses Ajax)

Get request operation five steps away ~

1: Creating an Ajax Object

2: Set Callback Object

3: Initializing Ajax objects

4: Send Ajax Object

5: Judgment and execution

Detailed parameters:

New XMLHttpRequest (): Calling Ajax Object

URL: Address of the past to transfer data

Xhr.status return status code, 200 for success other for failure

Xhr.responsetext: Callback Data

The code is as follows:

Window.onload=function () {

Get Event

(document.getElementById (XX) Get event

$ (' GO '). Onclick=function () {

var one=$ (' one '). Value;

var two=$ (' both '). Value;

var xhr=new xmlhttprequest ();

Set callback

xhr.onreadystatechange= function () {

if (xhr.readystate==4 && xhr.status==200) {

alert (Xhr.responsetext);

}

}

Set Header information

var url = ' 02.php?&one= ' + one + ' &two= ' + both + ' &_= ' + math.gethouse () ';

Initializing Ajax

Xhr.open (' get ', url);

Sending Ajax objects

Xhr.send (NULL);

}

}

(JQ mode using AJAX): most commonly used!

$.ajax ({

URL: ' xxx.php ',//Transfer past URL address

Type: ' Get ',//Pass way GET, POST

Data: {A:A,B:B},//Transfer past data {' Over the receiving ': ' On this Side '}

Success:function (msg) {

callback function msg

If it is a string, manipulate it directly

If it is JSON data, parse the ~ for (var i in msg) {} Using JSON data and then use msg[' a '] msg.a

If you want to print the data, use Console.log

}

})

Because the Internet and IE-compatible Ajax are not the same ~

Ajax to solve the compatibility problem, we can enter the following code in the JS file to

function Createxhr () {

In the Chinese.

Try{return new XMLHttpRequest ()} catch (e) {}

Ie

try{return new ActiveXObject (' Microsoft.XMLHTTP ')} catch (e) {}

Alert (' Your browser does not support Ajex ');

}

Ajax usage, implementation method, JS native and JQ implementations

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.