3

I am sending data to a c# web api controller by using the following call.

$.ajax({
    type: "POST",
    url: "menuApi/menu/Cost",
    data: JSON.stringify(order),
    contentType: "application/json",
    success: function (data) { window.alert('done')},
    dataType: 'json'
});

The c# controller on server side is like this:

public string Cost([FromBody] string order)
{
    var sOrder = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(order);

    return "";
}

The order object in Javascript is a complex object with nested arrays and properties. I am getting data as null. I am not sure how can I get the order sent through the ajax call.

Edit: This is my order object

var order = {
    name:"",
    id:"",
    cost:"",
    details: {
        sItem:[{name:"",cost:""}], 
        dItem:[{name:"",cost:"", components:[{name:"",quantity:""}]}]
    }
}
5
  • Please show definition of order Commented Dec 13, 2016 at 6:31
  • Try to use : application/json; charset=utf-8 Commented Dec 13, 2016 at 6:45
  • Try using a object as a parameter for your action rather then a string. MVC's mapping functionality should do its thing Commented Dec 13, 2016 at 6:55
  • Its not an mvc application. I have just added web api in simple asp.net project Commented Dec 13, 2016 at 7:07
  • @MichalHainc tried that but nothing changed. Commented Dec 13, 2016 at 7:07

2 Answers 2

2

Got it, you need to post the ajax request as form data with empty parameter/form field name like this:

    var order = {
        name: "",
        id: "",
        cost: "",
        details: {
            sItem: [{ name: "", cost: "" }],
            dItem: [{ name: "", cost: "", components: [{ name: "", quantity: "" }] }]
        }
    };
    $.ajax({
        type: "POST",
        url: "api/Values",
        data: {'': JSON.stringify(order)} ,
        success: function (data) { window.alert('done') },            
    });
Sign up to request clarification or add additional context in comments.

4 Comments

Its not working for me. The only problem, I am facing is that when actual object, which is a complex nested array object, the data I get is null. Even I tried your example with orderStr = "Hello"; $.ajax({ type: "POST", url: "menuApi/menu/Cost", data: { '': orderStr }, contentType: "application/json; charset=utf-8", success: function (data) { window.alert('done')}, });
Ok let me try 1:1
Yes got it, I didn't pay attention to the contentType. Yes it is working now
this is wonderful. Could not think of trying it with empty parameter. Still dont know the mechanics of why it is working now, but it is working.
0

The model binder's already done it for you, no need for deserializing. You just need to change parameter type to object or dynamic. Thing left is consideration to remove that redundant processing:

    public string Cost([FromBody] object order)
    {
        var sOrder = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(order.ToString());

        return "";
    }

1 Comment

There is nothing being done automatic here. It is not MVC project. I have added a web api and calling it from the ajax call.

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.