Sending data to C# web service from Jquery ajax
Suppose I have a web service/web method called service.asmx/Test. I have several methods in this web service.
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
}
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?
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; }
开发者_StackOverflow中文版 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
}
Thank you
Decided to pass several parameters instead of passing ONE parameter of MyCustom type.This solves both maintainability and flexibility issues.
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.
精彩评论