使用JQuery Ajax调用Asp.net Web方法 [英] Ajax call to Asp.net Web Method using Jquery

查看:125
本文介绍了使用JQuery Ajax调用Asp.net Web方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是jQuery的方法,从客户端发送信息(即只有会员识别#)到服务器端。

I am using a jquery method, to send information (namely only member identification #) from client side to the server side.

服务器侧已传统的Web方法实现,以便捕捉发送的数据,并执行基于它的SQL查询。

The server side has the traditional Web Method implemented so as to capture data sent and execute SQL queries based on it.

<一个href="http://www.aspsnippets.com/Articles/Make-AJAX-Call-to-ASP.Net-Server-Side-Web-service-method-using-jQuery.aspx"相对=nofollow>网络服务方法 - 使用 - jQuery的

不过到现在为止,我一直返回一个字符串从服务器端返回给客户端的SQL查询后。

However until now I have been returning a single string from the server side back to the client side after the SQL query.

想知道什么是返回一系列复杂的字符串...成员标识号的最佳方式,开始日期,结束日期,会员...取决于成员的类型的类型,可以有多个开始日期日期和结束日期。

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.

我应该寻找到XML?

Should I be looking into 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);
}

您可以使用 json.net 序列化.NET对象。

You can use json.net for serializing .Net objects

修改

您也可以做到这一点。

[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.

这篇关于使用JQuery Ajax调用Asp.net Web方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆