5

I am using jQuery.ajax(...) to retrieve JSON data from an ASP.NET MVC service. When the server encounters an exception, I send a 400 Bad Request status back to the client and send my exception as a JsonResult:

Response.StatusCode = 400;
return Json(new { ex.Message, ex.StackTrace });

And here's my jQuery code:

$.ajax(
{
    type: "POST",
    url: deleteUrl,
    dataType: "json",
    data:
    {
        dataItems: dataItems,
        toJSON: true
    },
    success: function(msg)
    {
        alert(msg[i].dataItem);
    },
    error: function(request, status, error)
    {
        alert(request.responseText);
    }
});

My ASP.NET code sends me to the error section of my JavaScript code, and the error block only allows me to read the request.responseText rather than work with the objects returned from the server.

Now, rather than add in yet another JavaScript include to something like json_parse and simply deserialize my Exception, I'd like to simply leverage the same JSON parser that jQuery uses, though I can't find readily find information on it.

Can someone point me in the right direction?

4
  • I found a post (west-wind.com/weblog/posts/324917.aspx) on Rick Strahl's blog that explains: "Note that the ajaxJSON function requires JSON encoding. jQuery doesn't have any native JSON encoding functionality (which seems a big omission, but was probably done to preserve the small footprint). However there are a number of JSON implementations available. Above I'm using the JSON2.js file from Douglas Crockford to serialize the parameter object map into JSON." I thought I had read this changed in jQuery 1.4...
    – Peder Rice
    Commented Jan 21, 2010 at 21:00
  • 2
    what changed was jQuery will now use JSON.parse instead of eval if it is available. jQuery never provided (and still does not provide) OOTB support for JSON serialization. Commented Jan 21, 2010 at 21:13
  • @Crescent, that should be given as an answer, not a comment.
    – Cheeso
    Commented Jan 21, 2010 at 21:18
  • @Cheeso: it's not an answer to the question. Commented Jan 21, 2010 at 21:30

3 Answers 3

6

jQuery used to use eval, if I'm not mistaken. Since 1.4, it takes advantage of native JSON deserializer if there is any (there is one in Firefox, for instance)

3
  • Anyone know if jQuery exposed a handy method for this?
    – Peder Rice
    Commented Jan 21, 2010 at 21:06
  • 5
    @Peder: yes, jQuery.httpData(request, "json") will give you back your parsed JSON/JavaScript object. Commented Jan 21, 2010 at 21:10
  • Actually, you don't have to specify "json"; httpData will default to that.
    – kgriffs
    Commented Aug 31, 2010 at 18:13
2

i think in javascript if you have a json string you can use eval to get an object, ie:

var myObject = eval('(' + myJSONtext + ')');

there is more information about this on http://www.json.org/js.html

3
  • Not the safest way to parse data.
    – Peder Rice
    Commented Jan 21, 2010 at 21:05
  • Actually, I take back what I said. Since in this case I'm only bringing back "safe" data and not user-provided data, I can safely deserialize the request data using eval.
    – Peder Rice
    Commented Jan 21, 2010 at 21:19
  • In $.ajax error function, you can then use : function (xhr) { json = eval('('+xhr.responseText+')'); ... };
    – Natim
    Commented Dec 15, 2011 at 10:35
0

Maybe this could help you. I use it to delete comments and spaces in my json :

json = eval( o.responseText
    .replace( /\/\*(.*)\*\/g, ' ' )
    .replace( /([^\:])\/\/[^\n]*\n/g, '$1' )
    .replace( /^\s|\s+|\s$/g, '' ) )
1
  • Comments aren't legal JSON anyway. Why are they being returned in your response? Commented Jan 22, 2010 at 1:57

Your Answer

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

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