0

I am attempting to send some data via JSON to a MVC controller action:

For some reason, this works:

var items = [];
$("input:checked").each(function () { items.push($(this).val()); });

//This works
$.ajax({
    type: "POST",
    url: url,
    data: { listofIDs: items, personID: personID},
    dataType: "json",
    traditional: true,
    success: function() {
        //Rebind grid
    }
});

//This listofIDs is ALWAYS null !? (longhand for `$.getJSON` ?)
$.ajax({
    url: url,
    dataType: 'json',
    data: { listofIDs: items, personID: personID },
    success: function () {
        //Rebind grid
    }
});

So why does it work at the top, but the bottom it is always null? The same code is used to build items !?

edit: controller method

public ActionResult AddJson(List<int> listofIDs, int personID)
        {
            if (listofIDs==null || listofIDs.Count < 1)
                return Json(false, JsonRequestBehavior.AllowGet);

...
//Add something to database
//Return true if suceeed, false if not
}

edit: so I ended up solving it by just turning the array into a string and sending it that way. That way I was able to send more than just the array variable.

var items = $(':input:checked').map(function () { return $(this).val();}).toArray();
            var stringArray = String(items);

$.ajax({
                url: url,
                dataType: 'json',
                data: { listOfIDs: stringArray, personID: personID },
                success: function () {
//rebind grid
                }
            });

Note no POST type needed to be set.

tereško
  • 58,060
  • 25
  • 98
  • 150
baron
  • 11,011
  • 20
  • 54
  • 88

3 Answers3

1

The problem is probably on the server side. In the first case you are using HTTP POST in the other HTTP GET. That means that you probably have to access the data differently. Maybe have a look at Get individual query parameters from Uri for the HTTP GET case.

Community
  • 1
  • 1
Ulrich Dangel
  • 4,515
  • 3
  • 22
  • 30
1

Without type: "POST" it defaults to GET (according to the docs), which your server side code is probably not expecting.

Also, you could get a list of the values with...

var items = $(':input:checked').map(function() {
    return $(this).val();
}).toArray();

jsFiddle.

Not sure if it's better though. Just an idea I had :)

alex
  • 479,566
  • 201
  • 878
  • 984
  • I added `type: POST` to the $.ajax and added `[HttpPost]` to controller method but the array was still null. – baron Aug 22 '11 at 01:25
1

You're not specifying the type of the request, so it defaults to GET.

From jQuery Docs:

The type of request to make ("POST" or "GET"), default is "GET".

Perhaps you meant to specify POST. If you send it using GET the array will be added into the QUERY_STRING like ?listofIDs=... and won't be accessible the same way you normally access POSTed data.

Paul
  • 139,544
  • 27
  • 275
  • 264
  • Is there a way I can send an array in the Query string like you've shown ? – baron Aug 22 '11 at 01:19
  • @baron The way you're doing it right now it will be sent in the query string. Like: `listofIDs[]=item1&listofIDs[]=item2&...` – Paul Aug 22 '11 at 01:24
  • Strange. Please see my edits. I have realized I need to send the list of ids as array and `personID` (it is the parent ID). But when I had `personID` also in the data, it just sent the `personID` so I asked for way to send array as query string. But then I commented `personID` entirely, and I got the query string as you've shown. But I could not get that in the controller method, `List listofIDs` was still null!? so I guess I need to work out to send it another way ? – baron Aug 22 '11 at 01:43