This is an MVC3 app. I have the following javascript call to my action:
function editDescription(docId,fileName, fileDescription) {
$.ajax({
type: "POST",
url: "/OrderDetail/LoadModelData",
contentType: "application/json; charset=utf-8",
data: "{'id': '"+docId +"', 'filename': '"+fileName+"', 'description': '"+fileDescription+"'}",
dataType: "json",
success: function (result) {
alert("ok: "+ result.d);
},
error: function (result) {
alert('Oh no: '+ result.responseText);
}
});
Heres my action:
[HttpPost]
public string LoadModelData(string id, string filename, string description)
{
return filename;
}
I run the code, the action gets called with the parameters, nothing is null, but the error function gets called every time. So the alert box with ‘Oh no’ in it appears every time, but the string being returned from the action is correct. If the filename is test.pdf the error alert box says
'Oh No: test.pdf'.
I looked in Firebug and there are no errors. Why isn’t the success function being called despite the fact there are no errors?
You are expecting (returning) a
stringvalue from your action method. Why do you need to specify the datatype asjsonthen ? Remove that and see what happens. And there is no d property from the response ! so just use result in the alert.the
datatypeproperty tells the server that what kind of content the client is expecting back as the result.EDIT : As Darin mentioned, Please Use the
JSON.stringifymethod to build the JSON request. Updating this answer to include correct way for future visitors.