Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am creating my first ASP.NET MVC2 Project with using jquery and ajax on client side.

Now when i was having Q.A testing the application and add some test exception and pass it to json from my controller that something like this:

Controller:

public JsonResult PopulateDetails(int id)
        {
                  ProductServiceClient client = new ProductServiceClient();
            try
            {
                if (id == 0)
                { throw new Exception("TEST ERROR."); }

                string name= "test";
                List<Product> product= client.GetPriductbyName(name);
                return Json(product);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

Jquery-Ajax Script:

$.ajax({
        type: "POST",
        url: url_ ,
        data: search,
        success: function(data) {   // data: Passed (Records) from the PopulateDetails controller
            displayDetails(data);   // after routing on Populate Details  converts the data to table
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert("error: " + XMLHttpRequest.responseText);
        },
        dataType: 'json'
    });

I notice that it will return this default error message below:

---------------------------
Windows Internet Explorer
---------------------------
error: <html>

    <head>

        <title>TEST ERROR.</title>

        <style>

         body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;} 

         p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}

         b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}

         H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }

         H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }

         pre {font-family:"Lucida Console";font-size: .9em}

         .marker {font-weight: bold; color: black;text-decoration: none;}

         .version {color: gray;}

         .error {margin-bottom: 10px;}

         .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }

        </style>

    </head>



    <body bgcolor="white">



            <span><H1>Server Error in '/' Application.<hr width=100% size=1 color=silver></H1>



            <h2> <i>TEST ERROR.</i> </h2></span>



            <font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">



            <b> Description: </b>An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.



            <br><br>



            <b> Exception Details: </b>System.Exception: TEST ERROR.<br><br>



            <b>Source Error:</b> <br><br>



            <table width=100% bgcolor="#ffffcc">

               <tr>

                  <td>

                      <code><pre>



Line 64:             catch (Exception ex)

Line 65:             {

<font color=red>Line 66:                 throw ex;

</font>Line 67:             }

Line 68:         }</pre></code>



                  </td>

               </tr>

            </table>



            <br>

---------------------------
OK   
---------------------------

Now how to display just the error message from the exception neatly not the whole html error message. Something like simple as display of an alert message box alert("TEST ERROR").

Is there ways how to do it more simpler?

I saw this linked on asp.net which look good How Do I Handle jQuery Errors and Display them on the Client?

but i cant find where AjaxResponseViewModel() came declared.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

In your catch, you should probably log the error and then return a json friendly result, e.g.

 catch (Exception ex)
 {
      //log.Error(ex); using whatever you use to log
      return Json(ex.Message);
 }

or

return Json("There has been an error, please contact support or read the log");

Above you are throwing the exception which will return the error view in full.

share|improve this answer
    
can you please use my controller method above so i can see where will i put it. Thanks. –  BizApps Nov 16 '12 at 9:20
    
another question how will i add this on error: on my ajax script. thanks –  BizApps Nov 16 '12 at 9:51
    
say you have a <div id="validationMessage" /> then instead of alert have $('validationMessage').html(XMLHttpRequest.responseText) –  dove Nov 16 '12 at 9:54
    
hi, had seen this was accepted, anything missing? –  dove Nov 19 '12 at 16:10
    
thanks...do you know some sample mvc app project that used wcf? –  BizApps Nov 20 '12 at 1:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.