-1

I have triggering an Ajax call and in the error callback I am trying to access the exception message(Not the Response Text). I am throwing an exception like this:

throw new Exception("Please enter a response")

Now I want to get the above message and display it in alert box.

I searched stackoverflow and found this:

 error: function(e,status){
                var err = eval("(" + e.responseText + ")");
                alert(err.Message);
            }

but the above doesn't work.

I am getting the response text but not able to access that particular message.

The error that I am getting is Uncaught SyntaxError: Unexpected token <

Vivek Sadh
  • 4,230
  • 3
  • 32
  • 49

2 Answers2

0

When your server throws an Internal Server Error, from Javascript side it's still a succes. how about adding the Status code response instead of throwing exception from the backend

 return new HttpStatusCodeResult(400, "Custom Error Message 2");
Shivang MIttal
  • 990
  • 1
  • 14
  • 36
  • 2
    I am able to catch the error in the error callback. Its not a success but I want to extract the exception message. – Vivek Sadh Jul 08 '15 at 12:37
0

You can refer this and try to get error message as alert(err.message); (lowercase) not alert(err.Message);

Community
  • 1
  • 1
Abhi
  • 4,123
  • 6
  • 45
  • 77