javascript 使用 Jquery 对 Asp.net Web 方法的 Ajax 调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14819607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ajax call to Asp.net Web Method using Jquery
提问by Philo
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 Method,以捕获发送的数据并基于它执行SQL查询。
Web-service-method-using-jQuery
Web-service-method-using-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 吗?
采纳答案by 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:
ASP:
[WebMethod]
public static string doSomething(int id)
{
....
DataTable dt = new DataTable();
dt = anothermethodReturningdt(id)
return JsonConvert.SerializeObject(dt);
}
You can use json.netfor 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.
关键是您可以序列化任何类型的对象、数组、集合等,然后将其传递回调用脚本。