ajax-(JQ,JS)

Source: Internet
Author: User
Tags http post

Ajax: Asynchronous Interactive transfer technology----is used to interact with the background data

Get request way: Pass through the URL address, its transmission data also in the URL address, generally is the string form transmits receives, the security is relatively not too high

Post request mode: through the browser internal transmission, usually in front of the Send () method to add the header file is setRequestHeader (), the data is passed by the Send () method by the Parameter form, pass data format is many, security relatively high

Send request (difference between get and post):

Send (data to be sent): Sending a request
Chinese code
Cache
Post:setrequestheader (type, content): Set request Header
"Content-type", "application/x-www-form-urlencoded"

Data type (processing of returned data):
The real data that the server returned to us
XML, HTML, JSON
The notation of JSON
Where to look when Eval parses JSON
Json.parse (): string parsed into Object

Reception of Back-end data:
$_get
-An array of variables passed to the script by URL
$_post
-An array of variables passed to the script via the HTTP POST method (form)
Front and rear key names and transmission must be consistent
Data transfer method
Data acquisition methods

var xhr = null;
try {
xhr = new XMLHttpRequest ();
} catch (e) {
xhr = new ActiveXObject (' microsoft.xmlhttp ');
        }

Xhr.open (' Post ', ' 2.post.php ', true);
Post mode, the data is placed in send () as a parameter pass
Xhr.setrequestheader (' Content-type ', ' application/x-www-form-urlencoded ');//Declaration of the type of data sent
Post does not have a cache problem
No coding required
xhr.send (' Username= Liu Wei &age=30 ');

Xhr.onreadystatechange= function () {

if (xhr.readystate= = 4) {
if (Xhr.status = = 200) {
AlertXhr.responsetext);
} else {
Alert (' Error, ERR: ' + xhr.status);
}
}

}

Get Cache issues:

Source: Each time the same URL address is sent, after the access request, there will be a cache, and then the same URL address, when the browser will go directly to read the cache, no longer send requests to the server

Workaround: ' 2.get.php?username=leo&age=30& ' +new date (). GetTime (), or add a math.random () random function; Connect a random number timestamp after the URL question mark

Get transfer Chinese has garbled problem: The URL transmits data using the encoded Encodeurl ' 2.get.php?username= ' +encodeuri (' Liu Wei ') + ' &age=30& ' +new Date (). GetTime ();

Post mode:

Xhr.setrequestheader (' Content-type ', ' application/x-www-form-urlencoded '); Declares the data type of data sent

Post mode, the data is placed in send () as a parameter pass

There is no Chinese garbled problem, because in the request header has been told the format of the background

ajax-(JQ,JS)

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.