0
$("#btnSend").click(function(){
    $.ajax({
       url         : "/ControllerName/MethodName",
       type        : "POST",
       dataType    : "json",
       contentType : "application/html",
       data        : '{ divhtml:"<p>World</p>" }'

    });
});

Passes null to the action method. Tried to change the content type to json. Started giving errors. What's the proper way?

4
  • Can you put what errors are u getting. Commented Aug 5, 2014 at 18:15
  • 2
    With JSON, keys are supposed to enclosed in quotes. Don't hardcode the string either, use JSON.stringify({key: value}) Commented Aug 5, 2014 at 18:15
  • Try using url: '@Url.Action("ControllerName", "ActionName")' and then let me know Commented Aug 5, 2014 at 18:15
  • @levi The error was: Invalid object passed in, ':' or '}' expected. (58): { divhtml:" Commented Aug 5, 2014 at 18:33

1 Answer 1

1

Change data: line. It should be. Without ''.

$("#btnSend").click(function(){
    $.ajax({
       url         : "/ControllerName/MethodName",
       type        : "POST",
       dataType    : "json",
       contentType : "application/json",
       data        : { 'divhtml':"<p>World</p>" }

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

2 Comments

Already went for stringify and sorted. But, I gave yours a try. Passing null. But it works if I change it to: data:"{ divhtml:'"<p>World</p>"' }", and contentType: "application/json".
May be because your response is in json and you were trying to catch a html ones. because that it may be worked when u changed it to: ontentType: "application/json"

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.