【问题标题】:how to get ajax response in html with out any html code attached to it如何在不附加任何 html 代码的情况下在 html 中获取 ajax 响应
【发布时间】:2013-12-19 00:50:18
【问题描述】:

我使用 jquery 和 ajax 从 html 页面调用 jsp 页面,它工作正常,但它显示的响应是一个 html 代码。见下文。我输入了 1 和 3,这两个数字的总和应该显示 4。但是在文本框中,它显示了带有结果的完整 html 代码。如何避免 html 代码............

第一个数字:1

第二个数字:3

回应

  !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title></head><body>4</body></html>

下面是我的html代码

AddNumbers.html

<body>
    <form id="form1">
    <h1> </h1>
        <h2>add two number</h2>
        <table>
            <tr>
                <td>first number:</td>
                <td><input type="text" id="num1" /></td>
            </tr>
            <tr>
                <td>second number:</td>
                <td><input type="text" id="num2" /></td>
            </tr>
            <tr>
                <td>result:</td>
                <td><input type="text" readonly id="result" />
                </td>
            </tr>
        </table>
        <br> <input type="button" id="Button1" 
            onclick="addNumbers()" value="add number" />
    </form>
</body>

AddNumbers.jsp

<body>
<% 
   int n1,n2;
n1=Integer.parseInt(request.getParameter("num1"));
n2=Integer.parseInt(request.getParameter("num2"));
System.out.print(n1+n2);
out.print(n1+n2);

%>    

test.js

function addNumbers() {
    alert("hello");
    $.get("AddNumbers.jsp",{num1:$("#num1").val(),num2:$("#num2").val()},doUpdate);
}

function doUpdate(response) {
    alert(response);
    if (response) {
        $("#result").val(response);
    }
}

【问题讨论】:

    标签: jquery html ajax jsp


    【解决方案1】:

    似乎 AddNumbers.jsp 包含 html(发布的代码显示正文标记)。从 AddNumbers.jsp 中删除所有 html 标记。

    【讨论】:

      【解决方案2】:

      只需删除所有 html 标记并仅添加下面的 java 代码

      <% 
      int n1,n2;
      n1=Integer.parseInt(request.getParameter("num1"));
      n2=Integer.parseInt(request.getParameter("num2"));
      System.out.print(n1+n2);
      out.print(n1+n2);
      
      %>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-07
        • 2022-06-10
        • 1970-01-01
        • 1970-01-01
        • 2019-07-26
        • 1970-01-01
        相关资源
        最近更新 更多