【问题标题】:ajax returns HTML source code instead of Stringajax 返回 HTML 源代码而不是 String
【发布时间】:2018-05-29 14:31:45
【问题描述】:

我有一个 web 项目,我想做的就是向 servlet 发送一个字符串,然后将相同的字符串返回到 jsp 页面(formList.jsp)并显示它:

发送字符串的javascript代码:

$(document).ready(function(){
    
    $('#submit').click(function() {
        var value = $('#val').val();
        $.ajax({
            type:'POST',
            data:{value:value},
            url:'FormList',
            success:function(result){
                $('#res').text(result);
            }
        });
    });
    
});

'FormList' Servlet doPost :

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/plain");
        String value = request.getParameter("value");
        response.getWriter().print(value);

}

web.xml(位于WEB-INF):

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee                       http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<servlet>
<servlet-name>FormList</servlet-name>
<servlet-class>FormList</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FormList</servlet-name>
<url-pattern>/FormList</url-pattern>
</servlet-mapping>
</web-app>

但是当我点击提交按钮时,我得到的结果是 formList.jsp 页面 HTML 源代码:(&lt;!DOCTYPE html PUBLIC ....)

*我正在重写 servlet 中的服务方法来初始化 formList.jsp

我认为 doPost 根本没有执行 请帮忙

【问题讨论】:

  • 您确定是在使用您的doPost 方法(您的ajax 请求的url 属性中的路径对吗?)或者您是否从服务器返回一个404/错误页面?跨度>
  • 以调试模式运行服务器并检查 dopost 是否正在执行并查看 String 值中的内容。如果您没有配备调试器,只需检查 system.out.println 值即可。
  • @83N,是的,它是正确的(FormList),我应该把它(/FormList)吗?
  • @ShubhamKadlag 我试过了,doPost 没有被执行:/,可能是什么原因?
  • @TamerNassar - 是的,我会检查路径是否正确/相对于请求它的页面 - 你的 doPost 应该只返回提交值的纯文本 - &lt;!DOCTYPE html... 表明服务器正在返回不同/错误页面...?

标签: java jsp servlets


【解决方案1】:

我强烈建议查看 BalusC 的回答 here 关于如何使用 servlet 进行 ajax 的问题。

我注意到你正在使用 jquery,你也可以像这样创建你的 ajax 请求:

<script>
$(document).on("click", "#submit", function() { 
    var value = $('#val').val(); //get value you want to send
    var params = {value : value}; //set it as one of the parameters

    $.post("FormList", $.param(params), function(response) { //make ajax request
                 $('#res').text(response); //retrieve response and set it to #res
    });
        });
</script>

但无论如何,您只看到 html 的原因是因为您没有将任何内容写回响应。你需要这样做:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String value = request.getParameter("value");


   response.setContentType("text/plain");  // Set content type of the response so that jQuery knows what it can expect.
    response.setCharacterEncoding("UTF-8"); // You want world domination, huh?
    response.getWriter().write(value);       // Write response body. (not print)
}

【讨论】:

  • 谢谢,response.getWriter().write(value);帮助了我:D
【解决方案2】:

给 FormList servlet 一个不同的 url 映射。

我猜想在解析 formlist.jsp 和 formlist servlet 时存在混淆。

【讨论】:

    猜你喜欢
    • 2017-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-06
    • 1970-01-01
    • 2017-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多