首頁  >  文章  >  web前端  >  關於Ajax中透過response在後台傳遞資料問題(含有程式碼,詳細解析)

關於Ajax中透過response在後台傳遞資料問題(含有程式碼,詳細解析)

亚连
亚连原創
2018-05-21 17:27:421490瀏覽

這篇文章跟大家介紹了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; 
}

上面是我整理給大家的,希望今後會對大家有幫助。

相關文章:

實作ajax取得跨域資料(圖文教學)

jquery的ajax和getJson跨域取得json資料(圖文教學)

jquery中ajax處理跨域的三大方式(圖文教學)

#

以上是關於Ajax中透過response在後台傳遞資料問題(含有程式碼,詳細解析)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn