AJAX request for ASP.NET web service - what type of parameter to use?

I have an array of JSON objects, some of which contain key / value pairs, for which this value is an array.

Example:

var jsonArray = [{ "key1":"value1", "key2":["value21", "value22"]},
                 { "key1":"value3", "key2":["value41", "value42"]}];

EDIT: Randomly used curly braces instead of brackets.

I am trying to send this via AJAX to an ASP.NET web service using jQuery:

$.ajax({
    type: "post",
    url: "example.asmx/SomeFunction"
    data: "{ 'items': '" + JSON.stringify(jsonArray) + "' }",
    contentType: "application/json;charset=utf-8",
    dataType: "json"
});

Is this the right way to send data? Also, what type of data do I need in a parameter SomeFunctionto accept and parse JSON data?

+3
source share
1 answer

Is it right to send data?

No, it would be better:

$.ajax({
    type: "post",
    url: "example.asmx/SomeFunction"
    data: JSON.stringify({ items: jsonArray }),
    contentType: "application/json;charset=utf-8",
    dataType: "json"
});

Also, what data type do I need in the SomeFunction parameter to accept and parse JSON data?

It will display on:

public void SomeFunction(IEnumerable<Foo> items)
{
    ...
}

Foo :

public class Foo
{
    public string Key1 { get; set; }
    public IEnumerable<string> Key2 { get; set; }
}

IEnumerable<T>, , , , T[].

+6

Source: https://habr.com/ru/post/1795886/


All Articles