JSON is a lightweight text data interchange format. It is independent of the programming language and can be used to interact with data between unused programming languages.
The following is a simple example of two data communication using JSON.
A first example:
//javascript send data JSON data in Ajax, PHP receives JSON//Front Endvararr ={ "Name": "Xiaoming", "Age": 16 };varJSON =json.stringify (arr);//using JSON to convert objects into JSON-formatted datavarXHR =NewXmlhttprequest;xhr.open (' Post ', './bb.php '); Xhr.setrequestheader (' Content-type ', ' application/x-www-form-urlencoded '); Xhr.send ("user=" + JSON);//when Content-type is set to application/x-www-form-urlencoded, the request body can send data in key1=value1&key2=value2 formXhr.onreadystatechange =function() { if(Xhr.readystate = = 4 && (xhr.status = = | | xhr.status ==304))//response is complete and the response code is 200 or 304alert (xhr.responsetext);} ------Gorgeous split-line----------------------------------------------------
//back end<?PHP $info= $_post["User"];//The info at this time is a string$result = Json_decode ($info);//The result of this time has been reverted to objectecho $result, name;
A second example:
//php send data JSON data JavaScript to receive JSON in AJAX // front end var xhr = new Xmlhttprequest;xhr.open ( ' post ', './bb.php ' ' content-type ', ' application/x-www-form-urlencoded ' = function () { if (xhr.readystate = = 4 && (xhr . Status = = 200 | | Xhr.status ==304) {// response complete and response code 200 or 304 Span style= "COLOR: #0000ff" >var rst = Json.parse (Xhr.responsetext); alert (rst.name); }};
------Gorgeous split-line----------------------------------------------------
// back end
$info = Array ("lession" = "English", "name" and "Lily");
echo Json_encode ($info);
Add a compatibility for JSON. JSON this built-in object does not exist before IE8, and if you need to use a JSON object before IE8, you need to introduce a third-party plug-in json2.js as described below. This will load the Json2.js plugin in IE7 and below, while other browsers or 8 and later versions of IE will not load this plugin:
<!--[If LTE IE 7]> <script src= "./json2.js" ></script><![ Endif]-->
Transfer between JS and PHP in JSON format