从Jquery Ajax将数据发送到C#Web服务 [英] Sending data to C# web service from Jquery ajax
问题描述
假设我有一个称为service.asmx/Test的Web服务/Web方法.我在此Web服务中有几种方法.
Suppose I have a web service/web method called service.asmx/Test. I have several methods in this web service.
我希望将一些数据发送到任何给定的方法.数据由几件事组成,每种方法可以将不同类型的变量发送给它.当前,我使用string [] []类型,因此我的Web服务如下所示:
I wish to send some data to any given method. The data is composed of several things, and each method can have different types of variables sent to it. Currently, i use string[][] type so my web service looks like this:
[WebMethod]
public void Test(string[][] data)
{
//do whatever
}
对我来说,这不是最好的方法.很多人使用IDictionary并定义强类型...但是看来这会增加更多的复杂性.正确的方法是什么?我应该使用哪种类型的变量,如List,string [] [],myCustomType?
it has been mentioned to me that it is not the best way to do it. A lot of people use IDictionary and define strong types...but it seems that it would add more complexity. What is the correct way to do it? What type of variable should I use, List, string[][], myCustomType?
如果我确实使用MyCustom类型,我想使其更通用,例如
If I do use MyCustom type, I would like to make it more generic such as
public class MyCustom {
public string user { get; set; }
public string domain { get; set; }
public string client { get; set; }
public string server { get; set; }
Array<Object> params;
}
其中数据是一个对象数组,每个对象可以具有不同的类型.
where data is an array of objects, each may be of a different type.
所以该方法现在看起来像:
So the method would now look like:
[WebMethod]
public void Test(MyCustom data)
{
//do whatever
}
谢谢
推荐答案
您可以做的是,可以使用JSON将数据发布到Web服务.同样在您的Web服务中,您必须添加以下内容: [System.Web.Script.Services.ScriptService] 然后,您可以做的是,将json对象用作ajax调用的数据,而asp.net将负责其余的工作.
What you can do is, you can use JSON to post data to your web service. Also in you web Service, you have to add this: [System.Web.Script.Services.ScriptService] Then what you can do is, you use json object as data of your ajax call and asp.net will take care of the rest.
这篇关于从Jquery Ajax将数据发送到C#Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!