This question has been asked many times but none of the solutions I found seem to be working. Makes me think this could be a new issue, and maybe something specific to ASP.NET MVC 3.
I am using JQuery Ajax to make a simple call to an ASP.NET MVC 3 controller. Like so
var addressInfo = {
Address1: "423 Judy Road",
Address2: "1001",
City: "New York",
State: "NY",
ZipCode: "10301",
Country: "USA"
};
$.ajax({
url: '/home/check',
type: 'POST',
data: JSON.stringify(addressInfo),
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function () {
alert("success");
},
error: function () {
alert("error");
}
});
The controller looks like this
[HttpPost]
public ActionResult Check(AddressInfo addressInfo)
{
// Do something and return Json(something)
}
This does not work, though it should have based on the "JavaScript and AJAX Improvements" section of Scottgu's post
I have tried many different variations of this, for example:
var args = new Object();
args.addressInfo = {
Address1: "423 Judy Road",
Address2: "1001",
City: "New York",
State: "NY",
ZipCode: "10301",
Country: "USA"
};
$.ajax({
url: '/home/check',
type: 'POST',
data: JSON.stringify(args),
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function () {
alert("success");
},
error: function () {
alert("error");
}
});
And then both of the above directly with the JSON object:
$.ajax({
url: '/home/check',
type: 'POST',
data: args,
success: function () {
alert("success");
},
error: function () {
alert("error");
}
});
None work. If I only have a string that I pass to the controller, that works. But as soon as I introduce an object, I cannot get it to work.
Anyone know what might be the issue.
Thanks a lot for looking into this!