views:

71

answers:

2

Does anyone have any insight on what's going on here? Here is my clientside jquery 1.4.1 code:

$.ajax({
    type: "POST",
    url: "PrintBOL/Print",
    data: [1, 2, 3],
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    error: function(xmlHttpRequest, status, errorThrown) {
        console.debug(xmlHttpRequest)
    },
    success: function(serverReply) {
        console.debug("OK")
        console.debug(serverReply)
    }
})

Here is my server-side method signature:

public ActionResult Print(int[] ids)

The ids parameter always comes across as null.

Any ideas?

By the way I make sure I invoke this at the top of the page:

 jQuery.ajaxSettings.traditional = true

UPDATE: See comments in Steven's answer below for resolution.

+2  A: 

You need to do:

data: { "ids[0]": 1, "ids[1]": 2, "ids[2]": 3},
Craig Stuntz
hmm, does var ids = [1,2,3]; JSON.stringify(ids) work? How do I get that serialization automatically?
George Mauer
Odd I just tried this - data: {"ids[0]": 1, "ids[1]": 2}, and it still came accross as null
George Mauer
Try changing the param to `IEnumerable<int> ids` -- that's how I do it.
Craig Stuntz
still nothing! How do I even go about debugging whats going wrong here?
George Mauer
Look at the data you're sending in Firebug's Net panel or Fiddler. If you still can't figure it out, add the form data to your question.
Craig Stuntz
George Mauer
George Mauer
Try it in a do-nothing, new MVC project. This should work. Something else is going on, here.
Craig Stuntz
+2  A: 

try the following:

change:

data: [1, 2, 3],

to

data: {"ids": [1, 2, 3]},
Steven Pardo
Didn't work. Odd.
George Mauer
does the Print Action have the POST attribute [AcceptVerbs(HttpVerbs.Post)]?
Steven Pardo
Just tried putting it there, still no good.
George Mauer
remove contentType: "application/json; charset=utf-8" and dataType: "json". JSON is the default for jQuery. Also, root your url: url: "/PrintBOL/Print".
Steven Pardo
Removing contentType: "application/json; charset=utf-8", did it! WTF?
George Mauer