Recently, I found that some of my colleagues like to create a new page when using ajax and data sources. in fact, I don't like this mode very much. The main reason is: first, it is difficult to maintain it in the future, what pages should I find, and second, I cannot call some original data methods on this page. therefore, I have made a test case here. Here, we have two ways to drop the data source method on this page. one is the webservice method. add the webservice method to this page. as follows:
[WebMethod]
Public static string GetWord (string arg)
{
Return "Call webService; Value:" + arg;
}
In this way, the method can be called before the client.
Var sdata = "http://www.cnblogs.com/incubator/archive/2011/12/09/#arg:'" + $ ("# txtVal"). val () + "'}";
$. Ajax ({
Type: "POST ",
ContentType: "application/json; UTF-8 ",
Data: sdata,
// DataType: "json ",
Url: "Default. aspx/GetWord ",
Success: function (msg ){
// Debugger;
// Var json = eval ('+ msg + ')');
Alert (msg. d );
},
Error: function (XMLHttpRequest, textStatus, errorThrown ){
Debugger;
Alert ("OK ");
// Usually in textStatus and errorThrown
// Only one containing information
// This; // The options parameter passed when calling this AJAX request
}
The second is to control whether to call the data source method through parameters.
The background code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
String methor = Request ["act"];
If (! String. IsNullOrEmpty (methor ))
{
This. GetType (). GetMethod (methor). Invoke (this, null );
}
}
Public void GetVal ()
{
String val = "getVal method to obtain parameters" + Request ["arg"];
Response. Clear ();
Response. Write (val );
Response. End ();
}
This is also a good method for calling data sources.
The front-end code is as follows:
Var data = http://www.cnblogs.com/incubator/archive/2011/12/09/new Object ();
Data. act = "GetVal ";
Data. arg = $ ("# txtVal"). val ();
$. Post ("Default. aspx", data, function (data) {alert (data );});
This is my experience. I hope you can talk more about it and see if there are any better methods to write ajax data sources.
[Csharp]
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. Services;
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
String methor = Request ["act"]
If (! String. IsNullOrEmpty (methor ))
{
This. GetType (). GetMethod (methor). Invoke (this, null );
}
}
Public void GetVal ()
{
String val = "getVal method to obtain parameters" + Request ["arg"];
Response. Clear ();
Response. Write (val );
Response. End ();
}
[WebMethod]
Public static string GetWord (string arg) 30
{
Return "Call webService; Value:" + arg;
}
}
Author: lx00002008