Manipulating JSON data using JavaScript Ajax communication [Top]

Source: Internet
Author: User

Previously just know the format of JSON, also done is to get the data from the database and then into the JSON format and then assign to the Highcharts generation curve, first the database data using the array () function to convert the arrays, and then use Json_encode () The function converts the array format to JSON format, which is then passed to the code in the Highcharts, resulting in a curve. (Json_code () and Json_encode () have the same effect)

Today strengthens the Json_code () function and the Json_decode () function of learning through $. Get () to get the data in the background and then output to the foreground.

Here is the code for the foreground client.html:

1 <HTML>2   <Head>3     <title>Initializing Web site basic information</title>4     <MetaCharSet= "UTF-8">5     <Metaname= "Viewport"content= "Width=device-width">6     <Scripttype= "Text/javascript"src= "Jquery.js"></Script>7   </Head>8   <Body>9         <Script>Ten           $("Document"). Ready (function() { One               varURL= "Server.php?inajax=1&do=checkmember&username=ericwolf"; A               varData={}; - $.get (Url,data,function(res) { -             varJsonobj=Eval ("("+Res+")"); the             $("#username"). Val (jsonobj.username); -             $("#groupname"). Val (jsonobj.groupname); -             $("#uid"). Val (jsonobj.uid); -               }); +           }); -         </Script> +  A         <Div> atUser name:<inputtype= "text"name= "username"ID= "username"> -             <BR/> -Member Group:<inputtype= "text"name= "GroupName"ID= "GroupName"> -             <BR> -Uid:<inputtype= "text"name= "UID"ID= "UID"> -   </Body> in </HTML>

where $. The res in the Get function (res) is the data that is returned to the foreground, since the returned data is converted to JSON using Json_code () in the background, and then is assigned to the form by jsonobj.username the obtained data.

Here is the code for the background server.php:

1<?PHP2 $inAjax=$_get[' Inajax '];3 $do=$_get[' Do '];4 $do=$do?$do: "Default";5 if(!$inAjax)return false;6 7 include' Db.class.php ' ;8 9 Switch($do) {Ten      Case"Checkmember": One           $username=$_get[' username ']; A           $sql= "SELECT * from Check_member WHERE username= '$username‘"; -           $result=$DBOBJ->getone ($sql); -            Echo(!Empty($result))? Json_encode ($result): "NULL"; the            Break; -      Case"Default": -            die("Nothing"); -            Break; +}

Where db,class.php is the code that connects the database and contains the GetOne () function, which is the function that gets a data from the database.

Enter client.html in the browser, and then view the returned data in Firebug such as:

Click the link to get:

The corresponding data is obtained from the database, and the format returned is in JSON format.

As you can see, there is no data in the original form, and now you have the data you have obtained:

Manipulating JSON data using JavaScript Ajax communication [Top]

Related Article

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.