3

In ASP MVC I have a controller that returns json data:

public JsonResult Edit(int? id)
{
   if(id==null)
   {
      Response.StatusCode = (int)HttpStatusCode.BadRequest;
      return Json(new { message = "Bad Request" }, JsonRequestBehavior.AllowGet);
   }

   car carTmp = db.car.Find(id);
   if (carTmp == null)
   {
      Response.StatusCode = (int)HttpStatusCode.NotFound;
      return Json(new { message = "Not Found" }, JsonRequestBehavior.AllowGet);
   }

   return Json(carTmp, JsonRequestBehavior.AllowGet);
}

I also have the following ajax request:

$.getJSON("Edit/" + data, function (result) {
            var car = result;
        })
        .error(function (error) {
            alert(error.message);
        })

Why, in case of success, in the result object I have the json object (i.e: I can access result.id, result.name ecc...) but in case of error, error.message is undefined? (I have the message into error.responseJson.message)

1
  • because the result is coming, i mean in case of null you are returning a error message but that too will be in case of success. You can validate it once by sending null to the model Commented Nov 6, 2015 at 10:46

3 Answers 3

1

You are setting the status code in the response as 404 so the error callback should be executed.

The problem is that the error callback is defined as function( jqXHR, textStatus, errorThrown ), where the jqxhr will have a responseJSON property.

You just need to change your code as:

$.getJSON("Edit/" + data, function (result) {
    var car = result;
})
.error(function (xhr) {
    alert(xhr.responseJSON.message);
})
Sign up to request clarification or add additional context in comments.

Comments

1

The problem is that you're setting the StatusCode wich is to be interpreted by the browser, but you're calling the server through an Ajax request, wich would be interpreted by the javascript XMLHttpRequest object in the lower layers, and it has the differnt behaviour that you've pointed out.

If you want an homogeneous way of getting the results don't force the status code on the response, that is, return everything as OK, and keep creating the messages as you do, in the end you're who stablish the protocols in your own application and you aren't breaking any rule.

Comments

0

The error callback will be executed when the response from the server is not going to be what you were expecting.

e.g. HTTP 404/500 error message(invalid ajax url), timeout....

In your case, you can add additional parameter 'success' to JSON response.

public JsonResult Edit(int? id)
{
   if(id==null)
   {
      return Json(new { success = false, message = "Bad Request" }, JsonRequestBehavior.AllowGet);
   }

   car carTmp = db.car.Find(id);
   if (carTmp == null)
   {
      return Json(new { success = false, message = "Not Found" }, JsonRequestBehavior.AllowGet);
   }

   return Json(new { success = true, data = carTmp }, JsonRequestBehavior.AllowGet);
}

And then in javascript you can handle your result like

$.getJSON("Edit/" + data, function (result) {
    if(result.success){
        console.log(result.data);
    } else {
        console.log(result.message);
    }  
})

2 Comments

I think adding Response.StatusCode = (int)HttpStatusCode.NotFound the ajax doesn't execute the success callback but the error....In your javascript you have put only the success callback
You can remove Response.StatusCode = (int)HttpStatusCode.NotFound and then handle your response in success callback

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.