Simple Example of JQuery Ajax request to implement partial refresh _ jquery

Source: Internet
Author: User
This article provides a detailed description of the simple example of JQuery Ajax request to implement partial refresh. For more information, see, we hope that the parameters (data) passed in the ajax path of the request will be received by a variable with the same name (with the set get method) in the action, the returned data is a JQuery array object. The data variables involved in the called action will encapsulate the objects in the data and finally return them to the page.

Case:



I want to change the status. After JQuery Ajax is used, the icon changes to partially refresh the page.

Principle:Partial refresh is part of the page refresh. In this case, only the icon changes are implemented, and the background code is separated from the icon in the foreground reality, instead of re-querying the database, second, after the digital display data in the background is modified, the foreground changes the icon directly.

1. The page provides a unique id value for the icon of each record:

The Code is as follows:











Ajax verification: add the id = aUnread to the a tag, and then add the event

The Code is as follows:


JQuery ("# aUnread"). click (function (){
Var strIds = ""; // defines a variable for data transmission.
$ ("Input [name = 'checkbox']"). each (function (){
If (this. checked ){
StrIds + = this. value + ","; // you can obtain Multiple id values and splice them into strings to upload them to the action.
}
});
$. Ajax ({
Type: "post ",
DataType: 'json', // accept the data format
Cache: false,
Data: "strIds =" + strIds,
Url: "$ {ctx}/feedbackonline/updateMessageStateUnread. action ",
BeforeSend: function (XMLHttpRequest ){
},
Success: function (data ){
Var str = data. str; // receives the returned data
For (var p in str) {// traverse the accepted array object
Var x = "# r" + str [p]; // retrieve the icon id of the record to be changed
$ (X). attr ("src", "$ {ctx}/images/04.png ");
// Change the src attribute value of the corresponding id value to the path of the corresponding icon
}
},
Error: function (){
// Handle request errors
Alert ("Error! ");
}
});
});


2. Background action:

The Code is as follows:


Private String strIds; // The set get method is omitted to automatically obtain the response data from the page.
Private String [] str; // omitting the set get Method
@ Action ("/updateMessageStateUnread ")
Public String updateMessageState () throws Exception {
String [] jStr = strIds. split (","); // splits the String into a String array.
Str = jStr; // assign the split string array to the array variable str with the get set Method and return it to the page.
For (int I = 0; I Int id = Integer. parseInt (jStr [I]);
MessageUserinfo = messageUserinfoManager. queryById (id );
MessageUserinfo. setMessageState (0 );
MessageUserinfoManager. update (messageUserinfo );
}
Return "ajax ";
}

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.