Comprehensive Analysis of Ajax comprehensive applications (full), comprehensive analysis of ajax

Source: Internet
Author: User

Comprehensive Analysis of Ajax comprehensive applications (full), comprehensive analysis of ajax

AJAX is "Asynchronous Javascript And XML" (Asynchronous JavaScript And XML), which is a Web page development technology used to create interactive web applications.

AJAX = Asynchronous JavaScript and XML (subset of standard General Markup Language ).

AJAX is a technology used to create fast dynamic web pages.

By performing a small amount of data exchange with the server in the background, AJAX can implement asynchronous updates on webpages. This means that you can update a part of a webpage without reloading the entire webpage.

If you need to update the content of a traditional web page (without AJAX), you must reload the entire web page.

• "Xml": returns an XML document, which can be processed by jQuery.

• "Html": returns plain text HTML information. The script tag is executed when the dom is inserted.

• "Script": returns plain text JavaScript code. Results are not automatically cached. Unless the "cache" parameter is set. Note: During remote requests (not in the same domain), all POST requests are converted to GET requests. (Because the script tag of DOM will be used for loading)

• "Json": Return JSON data.

• "Jsonp": JSONP format. When calling a function in the form of JSONP, such as "myurl? Callback =? "Will jQuery be replaced automatically? For the correct function name to execute the callback function.

• "Text": returns a plain text string.

1. the foreground transmits string variables and the background returns string variables (not in json format)

To solve the garbled characters in Ajax data transmission, the javascript function escape () is used to encode Chinese character strings and return

The unescape () function is used to decode the string so that the Chinese characters can be properly displayed. Of course, header files are added to the PHP code in the background to ensure that Chinese character strings do not contain garbled characters. Various background code Solutions

The problem of Chinese Character garbled characters is as follows:

PHP: header ('content-Type: text/html; charset = GB2312 ');

 Javascript code:

$ (Function () {var my_data = "foreground variable"; my_data = escape (my_data) + "; // encoding to prevent Chinese Character garbled characters $. ajax ({url: "ajax_php.php", type: "POST", data: {trans_data: my_data}, // dataType: "json", error: function () {alert ('error loading XML document');}, success: function (data, status) {// If php is successfully called alert (unescape (data); // decode, show Chinese characters }});});

 PHP code:

Header ('content-Type: text/html; charset = gb2312 '); // use gb2312 encoding, so that Chinese characters do not become garbled code $ backValue = $ _ POST ['Trans _ data']; echo $ backValue. "+ background return ";

2. the foreground transmits multiple one-dimensional arrays, and the background returns string variables (not in json format)

In a non-json format, only strings can be returned in the background. If you want to return arrays in the background, you can use json format. This article will detail later.

Javascript code:

$ (Function () {var my_data = new Array (); var my_data1 = new Array (); my_data [0] = 0; my_data [1] = 1; my_data [2] = 2; my_data1 [0] = 10; my_data1 [1] = 11; my_data1 [2] = 12; $. ajax ({url: "ajax_php.php", type: "POST", data: {trans_data: my_data, trans_data1: my_data1}, // dataType: "json", error: function () {alert ('error loading XML document');}, success: function (data, status) {// If php is successfully called alert (data );}});});

PHP code:

// Read the first array $ backValue = "trans_data:"; $ trans = $ _ POST ['Trans _ data']; foreach ($ trans as $ value) {$ backValue = $ backValue. "". $ value;} // read the second array $ backValue = $ backValue. ", trans_data1:"; $ trans =$ _ POST ['Trans _ data1']; foreach ($ trans as $ value) {$ backValue = $ backValue. "". $ value;} echo $ backValue;

3. the foreground transmits multiple one-dimensional arrays, and the background returns two-dimensional arrays (in json format)

Javascript code:

$ (Function () {var my_data = new Array (); var my_data1 = new Array (); my_data [0] = 0; my_data [1] = 1; my_data [2] = 2; my_data1 [0] = 10; my_data1 [1] = 11; my_data1 [2] = 12; $. ajax ({url: "ajax_php.php", type: "POST", data: {trans_data: my_data, trans_data1: my_data1}, dataType: "json", error: function () {alert ('error loading XML document');}, success: function (data) {// If php is successfully called} alert (back );}});});

PHP code:

Header ('content-Type: text/html; charset = gb2312 '); // uses gb2312 encoding to prevent Chinese characters from being garbled $ backValue = array (); $ backValue [0] =$ _ POST ['Trans _ data']; $ backValue [1] =$ _ POST ['Trans _ data1']; echo json_encode ($ backValue );

4. the foreground transmits one-dimensional arrays and two-dimensional arrays, and the background returns two-dimensional arrays (in json format)

Javascript code:

$ (Function () {var my_data = new Array (); var my_data1 = new Array (); var my_data2 = new Array (); my_data [0] = 0; my_data [1] = 1; my_data [2] = 2; my_data1 [0] = 10; my_data1 [1] = 11; my_data1 [2] = 12; my_data2 [0] = my_data; my_data2 [1] = my_data1; $. ajax ({url: "ajax_php.php", type: "POST", data: {trans_data: my_data, trans_data1: my_data1, trans_data2: my_data2}, dataType: "json", error: function () {alert ('error loading XML document');}, success: function (data) {// If php is successfully called} alert (back );}});});

PHP code:

Header ('content-Type: text/html; charset = gb2312 '); // uses gb2312 encoding to prevent Chinese characters from being garbled $ backValue = array (); $ backValue = $ _ POST ['Trans _ data2 ']; $ backValue [2] = $ _ POST ['Trans _ data']; $ backValue [3] = $ _ POST ['Trans _ data1 ']; echo json_encode ($ backValue );

The above content is a comprehensive analysis of Ajax comprehensive application Daquan, hope to help you!

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.