Http://wawa129.iteye.com/blog/1488221
1: Ajax constructs a URL by itself. In this way, passing parameters must use data and cannot be used for form submission.
Example:
Function createhtml (ID ){
$ ("# Reloading"). Show ();
// Edit_bg is a div that is displayed when submitted, so that the background page cannot be operated.
$ ("# Edit_bg"). Show ();
$. Ajax ({
Type: "Post ",
URL: "pageaction! Createhtml. Action ",
Data: "id =" + id,
Success: function (data ){
$ ("# Reloading"). Hide ();
$ ("# Edit_bg"). Hide ();
If (Data = "true "){
Alert ("operation successful. ");
} Else {
Alert ("operation failed. Please contact the administrator! ");
}
} // Actions after successful operations
});
}
2: Ajax submits a form, which is very useful for big data transmission, such as user registration, a large amount of information, submit a form using Ajax, the page is more beautiful
However, for Ajax form submission, we also need to introduce a JS, namely: jquery. Form. js"
Example: function uploadfile (){
$ ("# Reloading"). Show ();
$ ("# Edit_bg"). Show ();
// Form1 is the form name
$ ("# Form1"). ajaxsubmit ({
Type: "Post ",
Success: function (data ){
$ ("# Reloading"). Hide ();
$ ("# Edit_bg"). Hide ();
If (data. indexof ("true ")! =-1 ){
Alert ("operation successful. ");
} Else {
Alert (data );
Alert ("operation failed. Please contact the administrator! ");
}
} // Actions after successful operations
});
}
3: although the two Ajax submission forms can be correctly operated, the returned data is incorrect and the page cannot be processed. The following code corrects the Ajax submission form:
Function submitform (){
$ ('# Form1'). ajaxsubmit (ajaxoptionsnew );
}
VaR ajaxoptionsnew = {
Beforesubmit: fn_check_form,
Success: showresponse,
URL: "costation! Save2.action ",
Error: function (xmlresponse ){
Alert (xmlresponse. responsetext );
Alert ('Operation failed! ');
// Window. Location. Reload ();
}
};
Function fn_check_form (){
If ($ ("# name"). Val () = ""){
Alert ("cannot be blank ");
$ ("# Name"). Focus ();
Return false;
}
}
Function showresponse (responsetext ){
Try {
// Alert (responsetext );
If (responsetext = 'true '){
Alert ('Operation successful! ');
Window. Location. Reload ();
}
Else if (responsetext = 'paramfalse ')
{
Alert ("required parameter cannot be blank! ")
}
Else if (responsetext = 'timeerror ')
{
Alert ("the departure time cannot be later than the reimbursement time! ");
}
Else {
Alert ('Operation failed! ');
// Window. Location. Reload ();
}
} Catch (e) {alert (E. Message );}
}