简体   繁体   English

将int数组传递给Webservice

[英]Passing array of int to webservice

I have an $.ajax call to a .NET web service, which in turn leverages a function in a DAL down to the Entity Framework. 我有一个.NET Web服务的$.ajax调用,它依次利用了DAL中的一个功能,直到实体框架。 I think this will work, but the trouble is with passing the array of artist IDs. 我认为这可以解决问题,但是麻烦在于传递艺术家ID数组。 I have tried all sorts of crazy conversions from string arrays to int arrays, I've used Linq .Select and none of it works. 我已经尝试了从字符串数组到int数组的各种疯狂转换,但我使用了Linq .Select ,但都.Select I always get Cannot convert object of type System.String to type System.Int32[] or similar. 我总是得到Cannot convert object of type System.String to type System.Int32[]或类似Cannot convert object of type System.String to type System.Int32[] I understand the error, I just don't know how to fix it. 我理解该错误,只是不知道如何解决。

This is my jQuery: 这是我的jQuery:

                var params = new Array();
                $('ul li img.selected').each(function (index) {
                    params.push($(this).attr('id'));
                });

                var d = $('#calendar').fullCalendar('getDate').format();

                var parameters = {
                    sessionHash: 'xxx',
                    start: d,
                    end: d,
                    artists: JSON.stringify(params)
                };

                $.ajax({
                    type: "POST",
                    url: "/ws.asmx/GetDatesForArrayOfArtists",
                    data: JSON.stringify(parameters),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: (function Success(data, status) {
                        alert('hazzah!');
                    }),
                    error: (function Error(request, status, error) {
                        alert('poo botty bum smells');
                    })
                });

And this is my web service: 这是我的网络服务:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<Calendar> GetDatesForArrayOfArtists(string sessionHash, DateTime start, DateTime end, int[] artists)
        {
            var u = UserObject.GetCurrentUser(sessionHash);

            //var ints = artists.Select(x => int.Parse(x.ToString())).ToArray();

            return CalendarObject.GetDates(u.ClientId, start, end, artists);
        }

I've left some of my commented out code in, just so people can see my lame attempts at fixing this. 我保留了一些已注释掉的代码,以便人们可以看到我la脚的修复此问题的尝试。 So, the basic question is, given the code above, how can I pass an array of integers to the web-service? 因此,基本问题是,鉴于上述代码,我如何将整数数组传递给Web服务?

EDIT 1: Here are two screenshots of the browsers console, this may go a way to explain the problem: 编辑1:这是浏览器控制台的两个屏幕截图,这可能是一种解释问题的方法:

岗位响应

In your trace artists is array of arrays of strings - [["4","5"]] . 在您的跟踪artists是字符串数组- [["4","5"]]的数组。 I think it should be [4,5] to work. 我认为应该[4,5]

Consider changing adding parseInt to params.push($(this).attr('id')); 考虑更改将parseInt添加到params.push($(this).attr('id')); and removing JSON.stringify . 并删除JSON.stringify

Without knowing line numbers etc. of the problem you are having, perhaps this might help: 在不知道遇到问题的行号等的情况下,这可能会有所帮助:

If it doesn't could you please update your question with exactly where your code fails (which line and/or method). 如果不能,请更新代码的确切位置(哪行和/或方法),以更新您的问题。

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<Calendar> GetDatesForArrayOfArtists(string sessionHash, 
                          DateTime start, 
                          DateTime end, 
                          string[] artists)
    {
        var u = UserObject.GetCurrentUser(sessionHash);

        return CalendarObject.GetDates(u.ClientId, 
                   start, 
                   end, 
                   artists.Select(artist => Integer.Parse(artist)).ToArray());
    }

A simple way is: 一个简单的方法是:

In javascript build a new array: 在javascript中建立一个新的数组:

var myArray = new Array();
myArray.push([value1, value2, ...]);

In C# just create a ICollection parameter to get your matrix: 在C#中,只需创建一个ICollection参数即可获取矩阵:

[WebMethod(EnableSession = true)]
public void MyMethod GetMatrixFromJavascript(System.Collections.ICollection myArray) { 
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM