【问题标题】:To get the values in responseText in an AJAX call在 AJAX 调用中获取 responseText 中的值
【发布时间】:2012-04-13 10:31:40
【问题描述】:

我正在尝试使用 sendxmlRequest() 方法根据 eqid 从表中获取值:

 <select name="eqNAME" onchange="sendxmlRequest('GET','getEquipDetails.jsp',this.value)
 <options> ..... <options>
 </select>

这是我在 ajax.js 文件中添加的

//Make the XMLHttpRequest Object
var http = createRequestObject();
function sendxmlRequest(method, url,eqid){
    url = url + "?eqid="+eqid;
     if(method == 'get' || method == 'GET'){
    http.open(method,url,true);
    http.onreadystatechange = handleResponse;
    http.send(null);
    }
}    

function createRequestObject(){
    var req; try {
    // Firefox, Opera, Safari
    req = new XMLHttpRequest();
    } catch (e) {
    // Internet Explorer
    try {
    //For IE 6
    req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
    try {
    //For IE 5
    req = new ActiveXObject("Microsoft.XMLHTTP");
    }
         catch (e) {
     alert('Your browser is not IE 5 or higher, or Firefox or Safari or Opera');
     }
   }
}
return req;

}

以下用于处理响应(在 ajax.js 中):

function handleResponse(){
if(http.readyState == 4 && http.status == 200){
    var response = http.responseText;
}
}

这是我的“getEquipDetails.jsp”文件:

<% 
String planLoc= theResult1.getString(2) == null ? "":theResult1.getString(3);
String changLoc= theResult1.getString(3) == null ? "":theResult1.getString(4)
%> 
<%
response.setHeader("Pragma", "no-cache"); //HTTP 1.0
response.setDateHeader("Expires", 0); //prevents caching at the proxy server
response.setHeader("Cache-Control", "no-cache, private, no-store, max-stale=0"); //  HTTP 1.1
%>

我的问题是,我们如何从 getEquipDetails.jsp 中获取值 planLoc 和 changLoc 并将其设置在 responseText 中,以便可以在我的页面的下拉列表中对其进行更新?

或者还有其他方法可以解决吗?

注意:我没有给出表格检索代码,因为已经处理好了。我只想在我的 JSP 页面中更新 planLoc 和 changLoc

【问题讨论】:

    标签: javascript ajax jsp responsetext


    【解决方案1】:

    getEquipDetails.jsp

    写入值response.getWriter() 对象。

    <%
    response.setHeader("Pragma", "no-cache"); //HTTP 1.0
    response.setDateHeader("Expires", 0); //prevents caching at the proxy server
    response.setHeader("Cache-Control", "no-cache, private, no-store, max-stale=0"); //  HTTP 1.1
    PrintWriter res = response.getWriter();
    res.println(planLoc);
    res.println(changLoc);
    res.close();
    %>
    

    然后在

    function handleResponse(){
      if(http.readyState == 4 && http.status == 200){
        document.getElementById("dataTable").innerHTML = http.responseText;  
        // dataTable will be id of any HTML tag where you want to display the updated value.
      }
    }
    

    例如:

    <div id="dataTable"></div>
    

    因此,来自 AJAX 的响应将在此处设置。

    【讨论】:

      猜你喜欢
      • 2018-10-21
      • 2020-04-30
      • 2015-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多