2009年10月23日 星期五

com.googlecode.jsonplugin.JSONException: com.googlecode.jsonplugin.JSONException

頁面上使用jquery的ajax去call struts2的action


    $.ajax({
        url: 'xxxAtion.html',
        type: 'post',
        data: {
            'id' : "todd",
        },
        dataType: 'json',
        success: function(response, status) {
            var resultMessage = response.resultMessage;
            var resultStatus = response.status;
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert("系統繁忙, 請稍後再試!");
        }
    });


在action要return回去時, 不管怎樣都是跑到error

jquery的errro回傳textStatus寫 parsererror,
但發現吐回來的content有 com.googlecode.jsonplugin.JSONException


後來原因是 action中, 由spring去注入的xxxDAO, 寫了getxxxDAO, 拿掉就可以了

參考:
出現這種情況是在使用json-plugin時,一些不需要串行化的屬性被序列化了 比如spring 注入的 services生成了getter、序列化ID生成了getter等等 ,json plugin會把有getter方法的屬性序列化,格式化為json串 。解決辦法:可以去掉getter或者在getter上加上@JSON(serialize=false)

0 意見: