This time I will show you how Ajax transmits data in the background through response? (Code attached), what are the precautions for Ajax to transfer data in the background through response? Here is a practical case, let’s take a look.
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 functionsuccess, at first I return a json character directly in the background String , the result will be an exception, and there is no defined method or anything. After checking, I need to write data through the response.getWriter().write() method, and then the data can 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; }
Summary
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other php Chinese websites related articles!
Recommended reading:
How to implement WebApi Ajax cross-domain request using CORS
How to implement dynamic loading of combo box with Ajax (With code)
The above is the detailed content of How does Ajax pass data in the background through response? (with code). For more information, please follow other related articles on the PHP Chinese website!