Home > Web Front-end > JS Tutorial > Regarding the problem of passing data in the background through response in Ajax (including code, detailed analysis)

Regarding the problem of passing data in the background through response in Ajax (including code, detailed analysis)

亚连
Release: 2018-05-21 17:27:42
Original
1545 people have browsed it

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); 
  } 
};
Copy after login

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; 
}
Copy after login

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!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template