5

I have code that is part of an $.ajax call to MVC. Two functions that I hope will get executed based on a true or false from my controller.

success: function () {
    $('#mark').html('<span style='color: Green;'>&nbsp;&nbsp;Correct</span>');
},
error: function () {
    $('#mark').html('<span style='color: Red;'>&nbsp;&nbsp;Incorrect</span>')
}

But what should I return from the controller so I can call either the success or error? I didn't try Ajax before so can I just return

return(true) or 
return(false)

In which case what would be the datatype I use for the return value in MVC?

MikeSwanson
  • 497
  • 5
  • 7
  • 14
  • See the answer posted [here](http://stackoverflow.com/questions/2832510/how-to-return-an-error-in-an-ajax-scenario-from-asp-net-mvc-action/2844414#2844414). – Kevin Pang Apr 22 '11 at 15:10

5 Answers5

18

I prefer this technique over return Content("true")

return Json(true);

This way, you will get a boolean passed to your callback:

success: function (response) {
  if (response) {
    // success code here
  } else {
    // error code here
  }
}
Johnny Oshika
  • 54,741
  • 40
  • 181
  • 275
3

this "success" and "error" has nothing to do with the actual return value, but with the http return code. if your server returns a http 200 code, the request was sucessfull. otherwise if the server returns a 50x or 40x code than the request failed.

in your case you can actually return an empty object.

the fail codes are returned automatically by the server in cases there is an exception in your code, than it would be an error 501 code would be returned from server and the ajax request failed.

check this out http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

in your case you should do something like chaudcommeunnem has posted. but you also can leave your error passage, just in case the server failed the request.

hth

nWorx
  • 2,145
  • 16
  • 37
1

return Content("true");

You will need to either check for the string value or convert to boolean in your ajax handler

Keith
  • 5,311
  • 3
  • 34
  • 50
  • I can't find any reference on the web to "return Content". If I do this then where does the return value of "true" go to? Sorry but I don't understand. – MikeSwanson Apr 22 '11 at 15:23
0

I think the ajax "error" event is raised only if a problem happen in the controller action.

What you can do is to get a returned value from the controller and then call the function you want

success: function(data) {
             if  (data == "true"){
                 $('#mark').html('<span style='color:Green;'>&nbsp;&nbsp;Correct</span>');
             }
             else {
                 $('#mark').html('<span style='color: Green;'>&nbsp;&nbsp;Incorrect</span>');
             }
          }
0

javascript has "falsey" values which means all of the following will evaluate to false...

0, null, undefined, "", NaN

When you evaluate your result, do it like this...

if (result) { // true part here }
Burke Holland
  • 2,325
  • 1
  • 22
  • 33