4

In my controller I have

 public JsonResult GetInfo(string id)

in my js

 $.ajax({
        contentType: 'application/json, charset=utf-8',
        type: "POST",
        url: "/Incidents/GetInfo",
        data: { id: "777" },
        cache: false,
        dataType: "json",

        success: function (response) {
//etc....

jquery ajax error delegate gets executed. If I use

 data: { "777" },

no error, but the value doesn't get passed. This should be easy but I am beating my head against the wall. Maybe I am not allowed to pass strings to controller's actions?

What am I doing wrong here?

1
  • Which version of MVC are you using? If you are using MVC 2, you must install the JsonValueProviderFactory from MVC Futures. Commented Nov 16, 2011 at 3:10

2 Answers 2

12

You are indicating application/json request and you are sending a application/x-www-form-urlencoded request. So you will have to choose between one of the two ways to encode parameters and not mix them.

application/x-www-form-urlencoded:

$.ajax({
    type: "POST",
    url: "/Incidents/GetInfo",
    data: { id: "777" },
    cache: false,
    dataType: "json",
    ...
});

application/json:

$.ajax({
    type: "POST",
    url: "/Incidents/GetInfo",
    contentType: 'application/json, charset=utf-8',
    data: JSON.stringify({ id: "777" }),
    cache: false,
    dataType: "json",
    ...
});

The JSON.stringify method is natively built into modern browsers and is used to convert the javascript literal into a JSON string which is what we indicated that we are going to send the request as. If you are having to support legacy browsers you could include the json2.js script to your page which contains this method.

As a side note the dataType: "json" setting is not needed since the server will set the proper Content-Type header to application/json and jQuery is smart enough to use that.

And as a second side note you really don't want to be hardcoding an url like this in your javascript file: url: "/Incidents/GetInfo". What you want is to use url helpers when generating urls: url: "@Url.Action("GetInfo", "Incidents")".

Sign up to request clarification or add additional context in comments.

4 Comments

i will try json.stringify again - I think I did already, but never hurts. Initially I had something like var data = JSON.stringify(id) and then data:data. But it looks like I missed the id: part. I will try tomorrow and let you know.
@sarsnake, JSON.stringify(id) is wrong. The stringify method expects a javascript object: JSON.stringify({ id: id }) where you have defined the id variable previously. See my answer.
quick question, Darin - i used to use JSON.stringify all the time, but my impression was that doing something like {"id":"777"} is basically doing the same thing? Is it not? Looks like JSON format to me.
@sarsnake, it's absolutely not the same thing. It might look like JSON format to you but it is not. It is called javascript literal. jQuery transforms javascript literals using application/x-www-form-urlencoded. Use debugging tools such as FireBug and look at the request being send. When you use data: { foo: 'f', bar: 'b' } the actual request will look like this: foo=f&bar=b. JSON.stringify on the other hand converts the javascript literal into a JSON string. So data: JSON.stringify({ foo: 'f', bar: 'b' }) will be sent like this: { "foo": "f", "bar": "b" }. See? Nothing in common.
0

Are you missing HttpPost attribute in your action? If not, use something like firebug or chrome dev tools to see http request/response and get more details...

[HttpPost]
public JsonResult GetInfo(string id)

3 Comments

thanks I will try it. Hopefully this would shed the light on this mystery
It is not necessary to have this attribute and adding it will not solve the issue. This attribute enforces POST but actions that do not have it can be called with any verb.
@DarinDimitrov interesting...wasn't aware of that. thx for comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.