Home > Article > Web Front-end > Regarding the problem of passing data in the background through response in Ajax (including code, detailed analysis)
This article introduces to you the problem of transmitting data in the background through response in Ajax. Friends who need it can refer to it
This is the js code:
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); } };
When I want to get data through the callback function success, I directly return a json string in the background at first, and the result will be an exception. There is no method defined. Later, I checked and found that I need to pass The response.getWriter().write() method writes data, and the data can only be obtained in success. The background code is as follows:
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; }
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
Implement ajax to obtain cross-domain data (graphic tutorial)
jquery’s ajax and getJson cross Domain acquisition of json data (graphic tutorial)
##Three major ways to handle cross-domain ajax processing in jquery (graphic tutorial)
The above is the detailed content of Regarding the problem of passing data in the background through response in Ajax (including code, detailed analysis). For more information, please follow other related articles on the PHP Chinese website!