javascript 如何从div内的servlet获取响应数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6070087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 19:21:01  来源:igfitidea点击:

How to get response data from servlet inside a div?

javascripthtmlservlets

提问by AabinGunz

I have this simple html piece of code, whenever I click on any of the buttons the server will send back some string, I want it to be displayed in #responsediv.

我有这段简单的 html 代码,每当我单击任何按钮时,服务器都会发回一些字符串,我希望它显示在#responsediv 中。

<form target="_self" method="post">
    <table>
        <tr>
            <td width="35"><input type="button" id="btncreate" value="create" onclick="getData();"></td>
            <td width="35"><input type="button" id="btnupdate" value="update"></td>
            <td width="30"><input type="button" id="btndelete" value="delete"></td>
        </tr>
        <tr>
            <td>Name : </td>                    
            <td><input type="text" id="name" value=""></td>
        </tr>
        <tr>
            <td>File : </td>
            <td><input type="file" id="filname" value=""></td>
        </tr>
    </table>
</form>
<div id="response"></div>

In JS i am using XMLHttpRequestor ActiveXObjectbased on browser to send request to servlet, and i can get the response in client.responseText()so how can i direct this output to my #responsediv?

在 JS 中,我正在使用XMLHttpRequestActiveXObject基于浏览器向 servlet 发送请求,我可以获得响应,client.responseText()那么我如何将此输出定向到我的#responsediv?

Here is my JS

这是我的 JS

function getData()
{
var client;
var data;
var url_action="/temp/getData";
if(window.XMLHttpRequest)
{
    client=new XMLHttpRequest();
}
else
{
    client=new ActiveXObject("Microsoft.XMLHTTP");
}
client.onreadystatechange=function()
{
    if (client.readyState==4 && client.status==200)
    {
         document.getElementById("response").innerHTML=client.responseText; 
    }
};
data="name="+document.getElementById("name").value+"&file="+document.getElementById("filname").value;
client.open("POST",url_action,true);
client.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
client.send(data);
}

Servlet post method

servlet post方法

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    PrintWriter out=response.getWriter();
    log.info("Good");
    out.println("Good to go");
}

But using js script not jquery, i am not able to get the output from servlet

但是使用 js 脚本而不是 jquery,我无法从 servlet 获取输出

Thanks all for a jQuery code, any alternative in Javascript?

感谢大家提供 jQuery 代码,Javascript 中的任何替代方案?

采纳答案by diEcho

simply do

简单地做

success: function(data) { 
var res = client.responseText();
$('#response').html(res);
// or $('#response').append(res); 
});

in simple java script: document.getElementById("response").innerHTML=res;

在简单的java脚本中document.getElementById("response").innerHTML=res;

回答by Vadim

If you're using jQuery in your project:

如果您在项目中使用 jQuery:

$('#response').load("http://url...")

$('#response').load("http://url...")

http://api.jquery.com/load/

http://api.jquery.com/load/

回答by jwueller

Since you are using jQuery (assumtion based on the question tags), you can use the .load()-method:

由于您使用的是 jQuery(基于问题标签的假设),您可以使用.load()-method

$('#some-button').click(function (e) {
    e.preventDefault();
    $('#response').load('url/to/entry/point');
});

This has the benefit of jQuery doing all of the ajax work for you (creating and handling XMLHttpRequest and ActiveX stuff).

这有 jQuery 为您完成所有 ajax 工作的好处(创建和处理 XMLHttpRequest 和 ActiveX 内容)。