Comprehensive analysis of AJAX integrated Application (full) _ajax correlation

Source: Internet
Author: User
Tags foreach php code script tag

Ajax, "Asynchronous JavaScript and XML" (Asynchronous JavaScript and XML), refers to a web development technology that creates interactive Web applications.

AJAX = Asynchronous JavaScript and XML (a subset of standard generic markup languages).

AJAX is a technique for creating fast-moving web pages.

AJAX enables asynchronous updating of Web pages by making a small amount of data exchange in the background with the server. This means you can update portions of a Web page without reloading the entire page.

Traditional Web pages (without AJAX) if you need to update the content, you must overload the entire page page.

• "XML": Returns an XML document that can be processed with jQuery.

• "HTML": Returns plain text HTML information, and the included script tag executes when the DOM is inserted.

• "Script": Returns the plain text JavaScript code. Results are not automatically cached. Unless the "cache" parameter is set. Note: On remote requests (not in the same domain), all POST requests are converted to get requests. (because it will be loaded using the DOM's script tag)

• "JSON": Returns JSON data.

• "JSONP": Jsonp format. When calling a function using JSONP form, such as "myurl?callback=?" JQuery is automatically replaced? To the correct function name to execute the callback function.

• "Text": Returns a plain text string

One, front delivery string variable, background return string variable (non-JSON format)

Here, in order to solve the character garbled in the AJAX data transmission, the JavaScript function escape () is used to encode the Chinese character string before the string is passed, and the

String is decoded using the unescape () function, which enables the Chinese character to display correctly. Of course, the background PHP code also added a header file to ensure that the character string will not appear garbled. Various background code to solve

The problem of Chinese character garbled is as follows:

Php:header (' content-type:text/html;charset=gb2312 ');

 JavaScript code:

$ (function () {
var my_data= "foreground variable";
My_data=escape (My_data) + "";//encode to prevent Chinese characters from garbled
$.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 invoking PHP Success
alert (unescape (data);//decoding, displaying kanji
}
}
);

 PHP Code:

Header (' content-type:text/html charset=gb2312 '),//using GB2312 encoding, so that Chinese will not become garbled
$backValue =$_post[' Trans_data '];
echo $backValue. " + Backstage Return ";

Second, the front delivery of multiple one-dimensional array, background return string variable (non-JSON format)

In non-JSON format, the background can only return strings, if you want to return the array in the background, you can use the JSON format, described in detail later in this article.

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 invoking PHP success
alert (data);});

PHP Code:

Reads the first array
$backValue = "Trans_data:";
$trans =$_post[' Trans_data '];
foreach ($trans as $value)
{
$backValue = $backValue. " ". $value;
}
Reads the second array
$backValue = $backValue. ", Trans_data1:";
$trans =$_post[' trans_data1 '];
foreach ($trans as $value)
{
$backValue = $backValue. " ". $value;
}
Echo $backValue;

Three, front delivery multiple one-dimensional array, background return two-dimensional array (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 invoking PHP success
}
alert (back);
}
}
);

PHP Code:

Header (' content-type:text/html charset=gb2312 '),//using GB2312 encoding, so that Chinese will not become garbled
$backValue =array ();
$backValue [0]=$_post[' Trans_data '];
$backValue [1]=$_post[' trans_data1 '];
echo Json_encode ($backValue);

Four, front delivery one-dimensional array and two-dimensional array, background return two-dimensional array (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 invoking PHP success
}
alert (back);
}
}
);

PHP Code:

Header (' content-type:text/html charset=gb2312 '),//using GB2312 encoding, so that Chinese will not become 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 small series to introduce the comprehensive analysis of Ajax comprehensive application encyclopedia, I hope to be able to help everyone!

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.