How to pass an array string [] to a web service via jQuery?

here is my code:

var ids = $.map($("#container-1").children(), function(n, i) {
     return n.id;
});

$.ajax({
    type: 'POST',
    url: 'Loader.asmx/Active',
    data: "{'divs':'" + ids + "'}",
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function(msg) {}
});
+3
source share
2 answers

Javascript

var ids = $.map($("#container-1").children(), function(n, i) {
     return n.id;
});

$.ajax({
    type: 'POST',
    url: 'Loader.asmx/Active',
    data: { divs: ids.join("|") },
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function(msg) {}
});

in C #:

string[] arrIds = divs.Split('|');
+1
source

I would not think that you need to display your data as a string.

Does this work?

data: { divs: ids },

Assuming ids is a JavaScript string array, i.e.

0
source

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


All Articles