开发者

Callbacks on Ajax.BeginForm don't work right

开发者 https://www.devze.com 2023-03-02 23:59 出处:网络
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:

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:

  1. OnFailure="handleError" is the correct way to do this (see the other article I mentioned for resolution)
  2. 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
  3. 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 :(
0

精彩评论

暂无评论...
验证码 换一张
取 消