js eval() 解释json数据

使用ajax调用服务端数据时,得到返回的数据格式为json时,如:{"error":{"code":-1,"msg":"文件格式有误,请上传图片(gif,jpg,jpeg,png,bmp)文件"}}

对于这样的json格式的数据,可以使用js的eval()格式进行转化为json解释。

function upload(imgFile, image) { 
        $.ajaxFileUpload({ 
            url : '@{front.account.AccountHome.uploadPhoto()}', 
            secureuri : false, 
            fileElementId : 'imgFile', 
            dataType : 'text', 
            success : function(data) { 
                data = eval("("+data+")"); 
                if (data.error.code < 0) { 
                    alert(data.error.msg); 
                    return; 
                } 
                $("#image").attr("src", data.filename); 
                $("#imgFile").val(''); 
            }, 
            error : function(data, status, e) { 
                alert("上传图片失败"); 
            } 
        }) 
    }
posted @ 2015-05-11 09:16  划破的天空  阅读(321)  评论(0编辑  收藏  举报