C# 处理使用$.ajax(jquery)发布到Web服务的arrray

C# 处理使用$.ajax(jquery)发布到Web服务的arrray,c#,jquery,ajax,web-services,C#,Jquery,Ajax,Web Services,我正在尝试将数据发布到Web服务(asp.net 3.5),如下所示(两个变体,一个注释): 所以我需要一个匹配的webmethod来接收它。大概是这样的: [WebMethod] public string AddRoute(/* xxx */) { //handle data } 有人能详细说明一下我是如何获取数据的吗?我在这里输入了“xxx”? 我本以为“int[]array”会起作用,但它不起作用。 任何帮助都将不胜感激:)您应该公开一个asp.net mvc操作方法,而不是.

我正在尝试将数据发布到Web服务(asp.net 3.5),如下所示(两个变体,一个注释):

所以我需要一个匹配的webmethod来接收它。大概是这样的:

[WebMethod]
public string AddRoute(/* xxx */)
{
    //handle data
}
有人能详细说明一下我是如何获取数据的吗?我在这里输入了“xxx”? 我本以为“int[]array”会起作用,但它不起作用。
任何帮助都将不胜感激:)

您应该公开一个asp.net mvc操作方法,而不是.asmx web服务。Phil Haack的这篇文章展示了接受强类型json数据(包括数组)是多么容易:

我不熟悉ASP.NET,但是,应该有一些方法可以让您获得使用
jQuery.ajax()传递的请求参数

请求参数将是一些_数据。从控制器中,可以检索请求参数,而无需将其作为参数传递给函数


我很抱歉说得含糊不清,因为我来自Python/Pylons的背景。

请尝试以下代码:
您也可以使用int[]而不是string[]


[WebInvoke(UriTemplate=“ServiceName”,RequestFormat=WebMessageFormat.Json,
ResponseFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.Bare,
Method=“POST”)]

公共字符串ServiceName(string[]ids)

将您的服务类声明为

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[System.Web.Script.Services.ScriptService]
public class WFSrvc
{
    // To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
    // To create an operation that returns XML,
    //     add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
    //     and include the following line in the operation body:
    //         WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
    [OperationContract]
    public void DoWork()
    {
        // Add your operation implementation here
        return;
    }

    [OperationContract]
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    [System.Web.Script.Services.ScriptMethod(UseHttpGet = true)]
    public string HelloWorld()
    {
        return "Hello World";

    }
}


代替helloworld,您可以使用自己的函数

您的ajax帖子应该是这样的

$.ajax({
    type: 'POST',
    url: 'WebService2.asmx/AddRoute',
    data: '{arr:'+ JSON.stringify(array)+'}',
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function (msg) {
         console.log(msg.d);
         }
});
还有你的网络方法

[WebMethod]
public string AddRoute(int[] arr) //parameter name and the name in data:{} should be same
{
    //handle data
}

你可以作弊,将其声明为
对象
,使用调试器插入该方法并查看其实际类型。此外,根据JavaScript的注释,这可能是有用的R0MANARMY,Joel,非常感谢你的回复!我用一个web服务实现了“encosia”中描述的工作方式。所以我现在坚持这一点。不过,我稍后也会研究mvc。并不是说我打算接受任何错误:D,但如果我接受了,它们显然会更容易用mvc处理。再次感谢您抽出时间。我真的很感激!
$.ajax({
    type: 'POST',
    url: 'WebService2.asmx/AddRoute',
    data: '{arr:'+ JSON.stringify(array)+'}',
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function (msg) {
         console.log(msg.d);
         }
});
[WebMethod]
public string AddRoute(int[] arr) //parameter name and the name in data:{} should be same
{
    //handle data
}