3

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 Answers 2

4

try the following:

change:

data: [1, 2, 3],

to

data: {"ids": [1, 2, 3]},
3
  • does the Print Action have the POST attribute [AcceptVerbs(HttpVerbs.Post)]? Commented Feb 8, 2010 at 16:59
  • 3
    remove contentType: "application/json; charset=utf-8" and dataType: "json". JSON is the default for jQuery. Also, root your url: url: "/PrintBOL/Print". Commented Feb 8, 2010 at 17:39
  • Removing contentType: "application/json; charset=utf-8", did it! WTF? Commented Feb 8, 2010 at 18:00
2

You need to do:

data: { "ids[0]": 1, "ids[1]": 2, "ids[2]": 3},
8
  • hmm, does var ids = [1,2,3]; JSON.stringify(ids) work? How do I get that serialization automatically? Commented Feb 8, 2010 at 16:52
  • Odd I just tried this - data: {"ids[0]": 1, "ids[1]": 2}, and it still came accross as null Commented Feb 8, 2010 at 16:54
  • Try changing the param to IEnumerable<int> ids -- that's how I do it. Commented Feb 8, 2010 at 17:02
  • still nothing! How do I even go about debugging whats going wrong here? Commented Feb 8, 2010 at 17:20
  • 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. Commented Feb 8, 2010 at 17:33

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.