Here's an example: (json.html)
<HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Document</title> <Scriptsrc= "Js/jquery.js"></Script></Head><Body><inputtype= "button"value= "Submit"ID= "Submit"><DivID= "txt">&</Div><Script>//Listener Objectdocument.getElementById ('Submit'). onclick= function() {post ("json.php","name= Repetition Machine 1&age=123",function(data) {Console.log (data); },"JSON")}//a simple post packagefunctionPost (url,data,callback,datatype) {varXHR= NewXMLHttpRequest (); Xhr.onreadystatechange= function(){ if(Xhr.readystate== 4) {Callback (Json.parse (Xhr.responsetext)); }} xhr.open ("Post", URLs,true); Xhr.setrequestheader ("Content-type","application/x-www-form-urlencoded") xhr.send (data);}</Script></Body></HTML>
json.php
<? PHP $info Array ("Name" = "Repeater 2", "Age" =>223); $infoencode = Json_encode ($info); // convert to JSON format Echo $infoencode ;? >
If the php file is in gb2312 format, convert the value of the variable to utf-8 format, because the parameters of the Json_encode function must be utf-8:
<? php $info = array ("name" = "Repeater 2", "Age" = >223 foreach ( $info as $name = = $value $INFOGB [" $name "] = iconv (' gb2312 ', ' utf-8 ', $value );} $infoencode = Json_encode ( $INFOGB ); echo $infoencode ; ?
To summarize: The front-end back-end request sends data to the query string format, and the data to the front-end response is in JSON format for easy parsing with JavaScript.
Php+ajax+json