Alternatively AJAX and generic handler (.ashx)
Lili turns turns speaking in front of so many, mostly around PageMethod
(Send so much articles mediocre article, hurt everyone's eye sorry orz)
The reason AJAX call PageMethod would recommend the jQuery
In fact, the most important is through the mechanism of .NET auto Parse JSON
Let's write a program just before starting the program in a different AutoPostBack called called to the same page as
After a painful period beginning
Back to write is to fill the gap
But PageMethod has his shortcomings, for example, said that the amount of data to be transferred to the pure ajax
Many classes will be the output of js program automatically generated
And we actually have another choice, generic handler (.ashx)
Right select new projects can be seen
I believe some time to write a program people must know the generic handler, not introduced here, in short, it is a separate file of similar ASPX
Said the only benefit of using AJAX to call this page
The amount of data transmission is more than PageMethod small (relative time is faster)
Allows different page calls (in fact PageMethod also allows different page calls, but not recommended)
Let the program clearer division of labor
1. PageMethod: for that page will be used functions
2. ASHX: to allow different pages can be called with a program
My personal habits such points, so program logic modules with clearer, and indeed to make their programs DRY one o'clock
Direct cut to the chase, how to call
$.ajax({
url: {YourPath}/{YourFile}.ashx,
data: { itemId : 3 }, //不需要经过$.toJSON
type: 'GET',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (data) {
if(data)
alert(data.name);
}
});
url: file pointing to the ashx
data: can be placed directly into the object, or the group does not require string toJSON
type: GET (important, put the data directly if needed GET, or with but I did not try out orz; but not after the data processing can be put to use POST like it)
We can see the comparison with PageMethod is not the same as these three attributes
Of course, this is just a way of calling the ashx, jQuery is very powerful, there are other call mode
But I'm used to this, is relatively intuitive, and with json
In the program on this page is ashx
using System.Web.Services;
using System.Web.SessionState;
using System.Web.Script.Serialization;
public class CategoryHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string szItemId = context.Request.QueryString["itemId"] ?? string.Empty; //传入的参数
if (string.IsNullOrEmpty(szItemId)) return;
int nItemId = -999;
if (!int.TryParse(szItemId, out nItemId) || nItemId <= 0) return;
Student stu = new Student(nItemId);
context.Response.ContentType = "application/json";
context.Response.Charset = "utf-8";
context.Response.Write(new JavaScriptSerializer().Serialize(stu));
}
public bool IsReusable
{
get
{
return false;
}
}
}
You can see the method used is very simple, is to get value with queryString with incoming data attr (can pass multiple key -> value,)
Then perform
And after completing the operation, because this is not PageMethod, .net does not automatically parse JSON into
So we introduced manually using System.Web.Script.Serialization;
The statement said that these messages are then JSON, then put his tools into a JSON string Parse
So that you can receive the object in the local success of the
Particular attention is
success: function (data) {
if(data)
alert(data.name);
}
Properties can be seen here can operate directly on the data
Unlike PageMethod to get name
You must use data.d.name
This is where particular attention
Generally ashx I will get used to a similar procedure on the inside
For example, say I want to get a certain type of merchandise
I can write this program in ashx, then a different page to get different types of goods
I call on all this ashx, and in the process like, do not repeat on every page
As for how to write this program in conjunction with jQuery UI packed into a UI,
Each page only needs to be applied UI do not control AJAX, that is another story = =
The application also a lot ashx
After slowly introduced, if any wrong place also trouble predecessors correction
--
This article may have misunderstood or untrue place
Please do not hesitate to pass by senior swatted wake
This is the main nutrient of our growth
Original: Big Box Another option AJAX and generic handler (.ashx)