Callbacks on Ajax.BeginForm don't work right
why do I always have so much trouble...? given that I didn't solve the problem in my other article, I decided to just code the javascript right into the values... so I have:
OnSuccess="alert('ok')",
OnFailure="alert('failed')",
so my problem is the submission works fine; a record gets inserted into the database and I get a callback... but I get the wrong callback! I get a failure even though the record got inse开发者_JS百科rted. heeeeelp!
You should be able to read data from the response to figure out why it's considered a failure:
OnFailure="handleError",
...
function handleError(ajaxContext) {
var response = ajaxContext.get_response();
var statusCode = response.get_statusCode();
alert("Sorry, the request failed with status code " + statusCode);
}
Alternatively, use Fiddler and look at the response. Make sure the status code, content type and content are all as expected.
ok, I figured out a few things:
- OnFailure="handleError" is the correct way to do this (see the other article I mentioned for resolution)
- ajaxContext didn't have a get_response() method because I was actually hooking up the function to the OnComplete event instead (my bad)! once hooked up to the OnSuccess, I get my controller's method Json return value natively
- I was getting the OnSuccess handler called when the database entry was failing. this is because my controller method was try{} catch{}ing and therefore never failed! me being dopey :(
精彩评论