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]},
Sign up to request clarification or add additional context in comments.

3 Comments

does the Print Action have the POST attribute [AcceptVerbs(HttpVerbs.Post)]?
remove contentType: "application/json; charset=utf-8" and dataType: "json". JSON is the default for jQuery. Also, root your url: url: "/PrintBOL/Print".
Removing contentType: "application/json; charset=utf-8", did it! WTF?
2

You need to do:

data: { "ids[0]": 1, "ids[1]": 2, "ids[2]": 3},

8 Comments

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.