开发者

How does jQuery deserialize JSON?

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?


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)


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


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, '' ) )
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜