In. net, how does ajax call the data source on this page?

Source: Internet
Author: User

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

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.