JSON format in Ajax
HTML code:
PHP Code:
<?php
//receiving the request data sent by the client
$user = $_post[' user '];
is a JSON-formatted string
//var_dump ($user);
$json _user = Json_decode ($user, true);
Var_dump ($json _user[' name '));
$json = ' {' A ': 1, "B": 2, "C": 3, "D": 4, "E": 5} ';
Var_dump (Json_decode ($json));
The response data conforms to the JSON-formatted string
//1. Manually construct
//echo ' {' name ': ' Zhouzhiruo ', ' pwd ': ' 123456 '} ';
2. Use the Json_encode () function
echo json_encode ($json _user);
XML format in the second Ajax
HTML page:
PHP page code:
the request data sent by the <?php//Receiving client
$user = $_post[' user '];//a String type
//var_dump ($user) conforming to the XML format requirements;
Create a DOMDocument object
$doc = new DOMDocument ();
Call the Loadxml () method
$result = $doc->loadxml ($user);
Var_dump ($doc);
How to build XML-compliant data
/* Modify response header Content-type value is "Text/xml"
header (' Content-type:text/xml ');
echo $user//XML-formatted string type/
header (' Content-type:application/xml ');
echo $doc->savexml ();
HTML format in three Ajax
HTML page:
PHP Page:
<?php
//For processing client request two-level linkage data
//1. Receive the province information sent by the client
$province = $_post[' provcince '];
2. Determine current province information, provide different city information
switch ($province) {case
' Shandong Province ':
Echo ' Qingdao, Jinan, Weihai, Rizhao, Dezhou ';
break;
Case ' Liaoning Province ':
Echo ' Shenyang, Dalian, Tieling, Dandong, Jinzhou ';
break;
Case ' Jilin Province ':
Echo ' Changchun, Songyuan, Jilin, Tonghua, Siping ';
break;
The server-side response is a string
?>
The above content is small series to share Ajax three kinds of analytic mode, hope you like.