1

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 inserted. heeeeelp!

Community
  • 1
  • 1
ekkis
  • 9,804
  • 13
  • 55
  • 105

2 Answers2

2

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.

Danny Tuppeny
  • 40,147
  • 24
  • 151
  • 275
  • 1. I don't understand why OnFailure is a function call as opposed to a function name... I thought it was supposed to be OnFailure="handleError"? 2. ajaxContent doesn't seem to contain a get_response() method... it contains a responseText which is a string representation of the object my controller method returns (and which I can't dereference). it also contains fail, done, complete, success and error, all of which are methods that return the object itself... I'm using the latest version of the jquery.unobtrusive-ajax.min.js library right from the Microsoft CDN so what could be the issue? – ekkis May 06 '11 at 07:10
  • My bad, I copy/pasted. Fixed it - should be a function name (I believe). Check the content/content-type/status code with Fiddler to make sure it all looks correct. – Danny Tuppeny May 06 '11 at 12:12
0

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 :(
ekkis
  • 9,804
  • 13
  • 55
  • 105