In the previous article we obtained data from the background database and converted it into JSON format and then returned to the foreground, but just returned an array, this time we return two and three-dimensional arrays and objects.
Front Desk Code shizhan.html:
1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <title>JSON data acquisition</title>5 <MetaCharSet= "Utf-8">6 <Scriptsrc= ' Jquery.js '></Script>7 <Scripttype= "Text/javascript">8 $("Document"). Ready (function() {9 varURL="shizhan.php";Ten varData={" Do":" First"}; One A $.getjson (Url,data,function(res) { - $("#username"). Val (res.username); - $("#password"). Val (Res.password); the }); - - varData={" Do":"Second"}; - $.getjson (Url,data,function(res) { + $("#members"). Val (res.third.members.username); - }); + A varData={" Do":"Third"}; at $.getjson (Url,data,function(res) { - $("#address"). Val (res.address[1].username); - }); - - }); - </Script> in </Head> - <Body> to <H2>One array JSON data display</H2> + <inputtype= "text"name= "username"ID= "username" /><BR/> - <inputtype= "Password"name= "Password"ID= "Password"> the * <H2>Two-bit array JSON data display</H2> $ <textareaID= "Members"></textarea>Panax Notoginseng - <H2>Three-bit array JSON data display</H2> the <textareaID= "Address"></textarea> + </Body> A </HTML>
Here we use $.getjson (url,data,callback) to get the JSON-formatted data we return from the URL, noting that the JSON in $.getjson () must be capitalized.
And then we pass the parameters to the background without placing it at the end of the URL, but instead put it directly into the newly defined data variable. So the daemon gets the parameters no longer using $_get[], but uses $_request[].
It is also worth reminding that when the data in JSON format will be returned, if the first bit in the two-bit array in the background is [' 1 '], then the data we assign to the variable in the foreground should be in the same format as the one in the preceding code.
$ ("#address"). Val (res.address[1].username); Instead of $ ("#address"). Val (res.address.1.username) , and if the first bit of a two-dimensional or three-dimensional array in the background is [' third '], then the $ ("#" in the above code can be used Members "). Val (res.third.members.username); .
Background code:
1<?PHP2 3 $do=$_request[' Do '];4 5 $member[' username ']= ' mu class net ';6 $member[' Password ']= ' Mukewang ';7 8 $members[' 1 '] [' username ']= ' Zhang San ';9 $members[' 1 '] [' Password ']= ' Zhangsan ';Ten $members[' 2 '] [' username ']= ' John Doe '; One $members[' 2 '] [' Password ']= ' Lisi '; A $members[' 2 '] [' Address ']= ' Chaoyang District '; - - $members[' Third '] [' Members '] [' username ']= ' I'm a third user name '; the - classaddressclass{ - Public $address=Array(); - + Public functionSetaddress ($array) { - $this->address=$array; + } A at Public functionGetaddrss () { - return $this-address; - } - } - - $ADDRESSOBJ=NewAddressclass (); in - $ADDRESSOBJ->setaddress ($members); to + Switch($do) { - Case' First ':EchoJson_encode ($member); Break; the * Case' Second ':EchoJson_encode ($members); Break; $ Panax Notoginseng Case' Third ':EchoJson_encode ($ADDRESSOBJ); Break; -}
In the background code we use the Json_encode () function to convert an array, a two-dimensional array, a three-bit array, and an object into JSON-formatted data.
Manipulating JSON data using JavaScript Ajax [next]