I'm converting an existing ASP.NET app to MVC2, and I have an existing method that is called through jQuery using Ajax, that worked before, but does not work now. So it seems there are some change I need to do due to using MVC2 that I can't figure out.
I have reduced the complexity of the code, but it still do not work. This is my current code:
jQuery script that trigger on button click
function leaveComment() {
if (validate()) {
$.ajax({
type: "POST",
url: "/Pages/PostBlogComment",
data: "{'name':'Test','url':'Test','email':'Test','body':'Test','postid':'Test'}",
dataType: "json",
success: function (msg) {
//success code goes here
},
error: function (msg) {
//error code goes here
}
});
}
};
Inside my controller called Pages, I have created the following method:
public string PostBlogComment( string name, string url, string email, string body, string postid)
{
return "This is a test";
}
When debugging I can see that the PostBlogComment method gets called, but there are two major problems I'm facing here:
- All arguments to the method is received as null, so I have no useful data to work with. For testing now, all arguments are sent as
Testas you can see from the code. - When returning the result to the Ajax method, the error path is called, and not the success path, even it the method did return the string as normal (even if the parameters sent in was blank)
The error is probably easy to spot for those who work with these things regularly (or at least I hope so :))