简体   繁体   English

使用Jquery对Asp.net Web方法进行Ajax调用

[英]Ajax call to Asp.net Web Method using Jquery

I am using a jquery method, to send information (namely only member identification #) from client side to the server side. 我正在使用一种jquery方法,将信息(即仅成员标识号)从客户端发送到服务器端。

The server side has the traditional Web Method implemented so as to capture data sent and execute SQL queries based on it. 服务器端实现了传统的Web方法,以便捕获发送的数据并基于该方法执行SQL查询。

Web-service-method-using-jQuery Web服务方法使用jQuery

However until now I have been returning a single string from the server side back to the client side after the SQL query. 但是直到现在,在SQL查询之后,我一直从服务器端将一个字符串返回给客户端。

Wondering what would be the best way to return a complicated series of strings... member Identification number, start date, end date, type of member... depending on the type of the member, there can be multiple start dates and end dates. 想知道返回复杂的字符串系列的最佳方法是什么...成员标识号,开始日期,结束日期,成员类型...根据成员的类型,可以有多个开始日期和结束日期。

Should I be looking into XML ? 我应该研究XML吗?

What about returning even a datatable 甚至返回一个数据表怎么办

 $.ajax({
type: "POST",
url: "YourPage.aspx/doSomething",
data: "{'id':'1'}",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
    var returnedstring = data.d;
    var jsondata = $.parseJSON(data.d);//if you want your data in json
  }
});

aspx: ASPX:

[WebMethod]
public static string doSomething(int id)
{
   ....
   DataTable dt = new DataTable();
   dt = anothermethodReturningdt(id)

   return JsonConvert.SerializeObject(dt);
}

You can use json.net for serializing .Net objects 您可以使用json.net来序列化.Net对象

Edit 编辑

you can also do this 你也可以这样做

[WebMethod]
public static string doSomething(int id)
{
   Product product = new Product();
   product.Name = "Apple";
   product.Expiry = new DateTime(2008, 12, 28);
   product.Price = 3.99M;
   product.Sizes = new string[] { "Small", "Medium", "Large" };

   return JsonConvert.SerializeObject(product);
}

The point is you can serialize any type of object, arrays, collections etc and then pass it back to the calling script. 关键是您可以序列化任何类型的对象,数组,集合等,然后将其传递回调用脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM