为什么我从 jQuery 和 HttpHandler 获取 html 标签?
Why am I getting the html tags from jQuery and HttpHandler?
我有以下代码:
<script>
$(document).on('click','#Save',function()
{
$.get('My_App?New_Info='+$('button[id^=\"Current_\"]').text(),function(responseText)
{
$('#Page_div').text(responseText);
});
});
</script>
html 看起来像这样:
<div id=Page_div>
<Table Border=1>
<Tr>
<Td><button id=Current_1 type=button></button></Td>
<Td><button id=Current_2 type=button></button></Td>
<Td><button id=Current_3 type=button></button></Td>
<Td><button id=Current_4 type=button></button></Td>
<Td><button id=Current_5 type=button></button></Td>
<Td><button id=Current_6 type=button></button></Td>
<Td><button id=Save type=button>Save</button></Td>
</Tr>
</Table>
</div>
HttpHandler
代码如下所示:
public void handle(HttpExchange exchange)
{
OutputStream responseBody=exchange.getResponseBody();
String requestMethod=exchange.getRequestMethod(),requestPath=exchange.getRequestURI().getPath(),title="ABC",
New_Info,responseString="<Html>\n<Head>\n <Title>"+title+"</Title>\n</Head>\n<Body BgColor=#FDF5E6>\n<Center>\n"+Get_jQuery()+"\n\n";
LinkedHashMap<String,String> params=queryToMap(exchange.getRequestURI().getRawQuery());
File file;
try
{
New_Info=(params.get("New_Info")==null?null:URLDecoder.decode(params.get("New_Info"),"utf-8"));
if (New_Info!=null) // After Save button is clicked, save new user info
{
Headers responseHeaders=exchange.getResponseHeaders();
responseHeaders.set("Content-Type","text/html;charset=utf-8");
exchange.sendResponseHeaders(200,0);
responseString="<P><H2>Info [ "+New_Info+" ] saved.</H2>";
responseBody.write(responseString.getBytes());
}
...
}
一切正常,除了一件事,按钮被替换为来自 HttpHandler
的新文本,即字符串:"<P><H2>Info [ xx ] saved.</H2>"
但我希望它是大字体并且屏幕上的文本应该是:"Info [ xx ] saved." 而不是向用户显示 html 标签的 RAW 未渲染版本,为什么?我也试过了:
responseHeaders.set("Content-Type","text/plain");
没区别,我也试过用<html> ... </Html>
包围它,还是不行,正确的修复方法是什么?
text()
表示 "Treat the content as plain text".
如果您想将其视为 HTML 源代码,请改用 html()
。
我有以下代码:
<script>
$(document).on('click','#Save',function()
{
$.get('My_App?New_Info='+$('button[id^=\"Current_\"]').text(),function(responseText)
{
$('#Page_div').text(responseText);
});
});
</script>
html 看起来像这样:
<div id=Page_div>
<Table Border=1>
<Tr>
<Td><button id=Current_1 type=button></button></Td>
<Td><button id=Current_2 type=button></button></Td>
<Td><button id=Current_3 type=button></button></Td>
<Td><button id=Current_4 type=button></button></Td>
<Td><button id=Current_5 type=button></button></Td>
<Td><button id=Current_6 type=button></button></Td>
<Td><button id=Save type=button>Save</button></Td>
</Tr>
</Table>
</div>
HttpHandler
代码如下所示:
public void handle(HttpExchange exchange)
{
OutputStream responseBody=exchange.getResponseBody();
String requestMethod=exchange.getRequestMethod(),requestPath=exchange.getRequestURI().getPath(),title="ABC",
New_Info,responseString="<Html>\n<Head>\n <Title>"+title+"</Title>\n</Head>\n<Body BgColor=#FDF5E6>\n<Center>\n"+Get_jQuery()+"\n\n";
LinkedHashMap<String,String> params=queryToMap(exchange.getRequestURI().getRawQuery());
File file;
try
{
New_Info=(params.get("New_Info")==null?null:URLDecoder.decode(params.get("New_Info"),"utf-8"));
if (New_Info!=null) // After Save button is clicked, save new user info
{
Headers responseHeaders=exchange.getResponseHeaders();
responseHeaders.set("Content-Type","text/html;charset=utf-8");
exchange.sendResponseHeaders(200,0);
responseString="<P><H2>Info [ "+New_Info+" ] saved.</H2>";
responseBody.write(responseString.getBytes());
}
...
}
一切正常,除了一件事,按钮被替换为来自 HttpHandler
的新文本,即字符串:"<P><H2>Info [ xx ] saved.</H2>"
但我希望它是大字体并且屏幕上的文本应该是:"Info [ xx ] saved." 而不是向用户显示 html 标签的 RAW 未渲染版本,为什么?我也试过了:
responseHeaders.set("Content-Type","text/plain");
没区别,我也试过用<html> ... </Html>
包围它,还是不行,正确的修复方法是什么?
text()
表示 "Treat the content as plain text".
如果您想将其视为 HTML 源代码,请改用 html()
。