How does Ajax pass data in the background through response? (with code)

php中世界最好的语言
Release: 2018-03-31 10:52:39
Original
2607 people have browsed it

This time I will show you how Ajax transmits data in the background through response? (Code attached), what are theprecautionsfor 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); } };
Copy after login

When I want to get data through thecallback functionsuccess, at first I return a jsoncharacter 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; }
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!