Asynchronous transmission of ajax json data in jquery ajax instances

Source: Internet
Author: User

This article will introduce the interaction between ajax and json data. In the past, ajax post, get, AND htm and txt data were directly returned. This is a try to return json data.

In your own background cms, you want to use the pop-up mask layer on the same page to modify member information.

Getting UserID is a big problem

Finally, my colleague suggested using $. ajax.

The Code is as follows: Copy code

$ ('P. editInfo> A'). bind ('click', function (){
$ ('# Edit_uid'). val ($ (this). attr ("uid "));
// Alert ($ (this). attr ("uid "));
Var uid = $ (this). attr ("uid ");
// Alert (vote_content );
$. Ajax ({
Type: 'get ',
DataType: 'json ',
Url: 'user _ edit. php ',
Data: 'uid = '+ uid,
Success: function (json ){
$ ('# Username'). val (json. username); // assign a value to the corresponding josn.
$ ('# Mid_1'). val (json. mid );
$ ('# New_password'). val (json. password );
$ ('# Uid_edit'). val (json. uid );
},
Error: function (){}
});
});
});


User_edit.php is a normal php SQL statement.
Use the json_encode function (refer to the php-json_encode)

The Code is as follows: Copy code

$ Uid = $ _ GET ['uid'];
$ Query_admin_1 = $ db-> find ("table where uid = '$ uid '");
$ Row_admin_1 = $ db-> fetch_array ($ query_admin_1 );
$ Userinfo = json_encode ($ row_admin_1 );
Echo $ userinfo;


The resulting $ userinfo is in json data storage format.

Standard html form for receiving json data

The Code is as follows: Copy code

<Div class = "txt-Example">
<Label for = ""> User Name </label>
<Input id = "username" class = "good_input" name = "username" type = "text"/>
</Div>
<Div class = "txt-Example">
<Label for = ""> User Password/label>
<Input id = "new_password" name = "new_password" type = "password"/>
</Div>

Note that the ajax operation is slightly different from other operations, but only in dataType: 'json'. Then we can use json. the content accessed in the form of mid is actually in the form of an array $ a ['a']. json is a returned array, and mid is the id we return.

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.