ann*_*ann 0 ajax jquery servlets
我正在使用 jquery ajax 来发送和接收来自 servlet 的数据。例如:
$(document).ready(function(){
$('button').click(function(){
$.post('/test/hello',{no:'5'},function (response){
$('#div').text(response);
});
});
});
Run Code Online (Sandbox Code Playgroud)
这是我在 hello.java servlet 中的代码
String no = request.getParameter("no");
int n = Integer.parseInt(no);
for (int i = 0; i < n; i++) {
out.println("hello<br/>");
}
Run Code Online (Sandbox Code Playgroud)
但是,当我收到 servlet 的响应时,hello 后跟的 Break 标记会在屏幕上连续一行打印 5 次,即 Break 标记不会解释为 html,而是解释为文本。
如何让 Jquery 将中断标记解释为新行,以便获得这样的输出?
你好
你好
你好
你好
你好
由于您在响应中发送 HTML,.html()而不是.text().
$('#div').html(response);
Run Code Online (Sandbox Code Playgroud)