1

I have such JSON string: {"1":[1,3,5],"2":[2,5,6],"3":[5,6,8]}

I want to send it to the Web Api Controller without changing using ajax request:

$.ajax({ type: "POST", url: "Api/Serialize/Dict", data: JSON.stringify(sendedData), dataType: "json" });

In Web Api I have such method:

[HttpPost]
public object Dict(Dictionary<int, List<int>> sendedData)
{
    var d1 = Request.Content.ReadAsStreamAsync().Result;
    var rawJson = new StreamReader(d1).ReadToEnd();
    sendedData=Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<int, List<string>>>(rawJson);
    return null;
}

But rawJson always is empty string. I don't understand why? But d1.Length is the same as in JSON string. I don't know how to get JSON string from d1...

Thank you!

5
  • try using [FromBody] attribute public object Dict([FromBody] Dictionary<int, List<int>> sendedData) Commented Apr 10, 2014 at 18:37
  • I tried - the same situation... Commented Apr 10, 2014 at 18:39
  • replace type of sendedData with object and see what you indeed get there Commented Apr 10, 2014 at 18:45
  • use content type parameter instead of dataType when performing ajax call: contentType: "application/json; charset=utf-8" Commented Apr 10, 2014 at 18:48
  • Thank You! I even don't know, how I missed it! It works! Commented Apr 10, 2014 at 18:56

1 Answer 1

1

use content type parameter instead of dataType when performing ajax call:

$.ajax({ 
       type: "POST",
       url: "Api/Serialize/Dict", 
       contentType: "application/json; charset=utf-8", //!
       data: JSON.stringify(sendedData) 
});
Sign up to request clarification or add additional context in comments.

Comments

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.