来自java servlet的Jquery unescape响应文本无法正常工作

时间:2015-05-14 23:49:36

标签: java jquery ajax servlets atg

java代码

 pResponse.setHeader("content-type", "text/plain;chartset=UTF-8");
        pResponse.setContentLength(resultsJson.length());
        Writer out = pResponse.getWriter();
        String escapedJsonString = StringEscapeUtils.escapeJavaScript(resultsJson);
        out.write(escapedJsonString);

逃避返回文本的目的是因为在' resultsJson'中有一些重音字符。即使我设置charset = UTF-8,我仍然会从ajax中获取乱码文本。从我aggregate

查看此问题

ajax代码

var option = {
        type : 'POST',
        url : $('#csrOrderExportform').attr('action'),
        data : $('#csrOrderExportform').serialize(),
        beforeSend : preAjaxReqest,
        dataType:'text',
        error : function(data, statustext, error){
            $(".searchSubmitBtn").removeAttr("disabled");
            setErrorMessage('No Records Found!');
        },
        success : function(data){
            if (data) {
                alert(unescape(data));}
        }
    };
    $.ajax(option);

回复文字

[{\"orderNumber\":\"S301020000\",\"customerFirstName\":\"\u5F20\u79D1\",\"customerLastName\":\"\u5F20\u79D1\",\"orderStatus\":\"PENDING_FULFILLMENT_REQUEST\",\"orderSubmittedDate\":\"May 13, 2015 1:41:28 PM\"}]

从jquery中取消文本后,我得到了相同的文字。

预期输出

[{"orderNumber":"S301020000","customerFirstName":"张科","customerLastName":"张科","orderStatus":"PENDING_FULFILLMENT_REQUEST","orderSubmittedDate":"May 13, 2015 1:41:28 PM"}]

1 个答案:

答案 0 :(得分:0)

这应该有效: -


    pResponse.setContentType("application/json");
    pResponse.setContentLength(resultsJson.length());
    Writer out = pResponse.getWriter();
    String escapedJsonString = StringEscapeUtils.escapeJavaScript(resultsJson);
    out.println(escapedJsonString);