Reencapsulate ajax in Jquery to simplify the operation example

Source: Internet
Author: User

Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> example of jQueryAjaxJson value </title>
<Script type = "text/javascript" src = "Scripts/jquery-1.4.4.min.js"> </script>
<Script type = "text/javascript">
$ (Function (){
JsonAjax ("AjaxQuery. aspx", "type = json", "json", callBack );
JsonAjax ("AjaxQuery. aspx", "id = 1 & name = 2 & type = text", "text", callBackTxt );
});

Function callBack (data ){
$ ("# Ddd" ).html ('');
Var json = eval (data); // Array
$. Each (json, function (index, item ){
// Obtain data cyclically
Var name = json [index]. Name;
Var age = json [index]. Age;
Var sex = json [index]. Sex;
$ ("# Ddd" ).html ($ ("# ddd" ).html () + "<br>" + name + "" + age + "" + sex + "<br/> ");
});
};
Function callBackTxt (data ){
$ ("# Ccc" pai.html (data );
};

/**
* Ajax post submission
* @ Param url
* @ Param
* @ Param datat: html, json, and text
* @ Param callback function
* @ Return
*/
Function jsonAjax (url, param, datat, callback ){
$. Ajax ({
Type: "post ",
Url: url,
Data: param,
DataType: datat,
Success: callback,
Error: function (){
JQuery. fn. mBox ({
Message: 'recovery failed'
});
}
});
}

</Script>
</Head>
<Body>
<Span id = "ccc"> </span>
<Span id = "ddd"> </span>
</Body>
</Html>

Copy codeThe Code is as follows:
Using System;
// Add
Using System. Web. Script. Serialization;
Using System. Collections. Generic;

Public partial class AjaxQuery: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
// Data simulation, for reference only
String messgage = string. Empty;
String id = Request ["id"];
String name = Request ["name"];
String gettype = Request ["type"];
If (gettype = "text ")
{
Messgage = (id = "1" & name = "2 ")? "OK meets the condition": "sorry does not meet the condition ";
}
Else if (gettype = "json ")
{
List <Student> list = new List <Student> ();
For (int I = 0; I <50; I ++)
{
Student a = new Student ();
A. Name = "Zhang San" + I;
A. Age = I;
A. Sex = "male ";
List. Add ();
}
Messgage = new JavaScriptSerializer (). Serialize (list );
}
Else
{}
Response. Write (messgage );
Response. End ();
}
}
Public struct Student
{
Public string Name;
Public int Age;
Public string Sex;
}
}

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.