To encapsulate Ajax in jquery, simplify the operation example _jquery

Source: Internet
Author: User

Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title>jqueryajaxjson Sample </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) {
Looping through data retrieval
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"). HTML (data);
};

/**
* Ajax Post Submission
* @param URL
* @param param
* @param datat for Html,json,text
* @param callback 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: ' Restore failed '
});
}
});
}

</script>
<body>
<span id= "CCC" ></span>
<span id= "DDD" ></span>
</body>


Copy Code code as follows:

Using System;
New
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 not meet the condition";
}
else if (GetType = "JSON")
{
list<student> list = new list<student> ();
for (int i = 0; i < i++)
{
Student a = new Student ();
A.name = "John" + I;
A.age = i;
A.sex = "male";
List. Add (a);
}
Messgage = new JavaScriptSerializer (). Serialize (list);
}
Else
{ }
Response.Write (Messgage);
Response.End ();
}
}
public struct Student
{
public string Name;
public int age;
public string Sex;
}
}

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.