Ajax call to Asp.net Web Method using Jquery

﹥>﹥吖頭↗ 提交于 2019-12-04 19:55:01
ZedBee

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:

[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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!