Home  >  Article  >  Backend Development  >  Analyze the problem of passing data in the background through response in Ajax

Analyze the problem of passing data in the background through response in Ajax

小云云
小云云Original
2017-12-25 10:47:191303browse

An exception will occur when we pass data in the background through response in Ajax. How to solve it? This article will introduce to you the analysis of the problem of transmitting data in the background through response in Ajax. Friends who need it can refer to it. I hope it can help you.

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, at first I return a json string directly in the background, and the result will be an exception, which is not defined After checking the method or something, I found that the data needs to be written through the response.getWriter().write() method, and 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; 
}

Have you learned it? Hurry up and give it a try.

Related recommendations:

Talk about the use of the two objects Request and Response

AngularJS’s ng Http Request and response format conversion Method

PHP and AJAX responseXML Example

The above is the detailed content of Analyze the problem of passing data in the background through response in Ajax. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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