关于Ajax中通过response在后台传递数据问题

这是js代码:

var System = { 
  getHttpRequest: function(url, callback, options) { 
    if (url.length < 0) return; 
    var option = { 
      url: url, 
      type: "get", 
      dataType: "json", 
      cache: false, 
      timeout: 30000, 
      beforeSend: function(XHR) { 
      }, 
      complete: function(XHR, textStatus) { 
        XHR.abort(); 
      }, 
      error: function(XMLHttpRequest, textStatus, errorThrown) { 
        //alert("网络连接不通,请稍后再试!"); 
      }, 
      success: function(data) { 
        callback(data, options); 
      } 
    }; 
    if ( !! options) { 
      option = $.extend(option, options); 
    } 
    $.ajax(option); 
  } 
};

当我想要通过回调函数success获取data时,一开始我是直接在后台return一个json字符串,结果会报异常,没定义方法什么的,后来查了下,需要通过response.getWriter().write()方法写入数据,success中才能获取到数据。后台代码如下:

public String getRejectReason() throws Exception{ 
    String rowId = getParameterAndPut("rowId",null,0).toString(); 
    String jsonData = ""; 
    if (StringUtils.isNotEmpty(rowId)) { 
      jsonData = newOwnerInfoService.getRejectReasonJsonData(rowId); 
    } 
    this.getResponse().setCharacterEncoding("utf-8"); 
    this.getResponse().getWriter().write(jsonData); 
    return null; 
}

总结

相关推荐