admin管理员组文章数量:1400174
I want a servlet to process GET request and return a string.
Very simplified version is:
public class handlequery extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text");
PrintWriter out = response.getWriter();
out.println("videoid");
}
}
But the return data
(which I check as follows) in the callback is - object XML Document
.
$.get("handleq", function(data, textStatus) {
alert("Done, with the following status: " + textStatus + "." +
" Here is the response: " + data);
});
Can someone tell me why data is object XML Document
when I should get videoid
?
I want a servlet to process GET request and return a string.
Very simplified version is:
public class handlequery extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text");
PrintWriter out = response.getWriter();
out.println("videoid");
}
}
But the return data
(which I check as follows) in the callback is - object XML Document
.
$.get("handleq", function(data, textStatus) {
alert("Done, with the following status: " + textStatus + "." +
" Here is the response: " + data);
});
Can someone tell me why data is object XML Document
when I should get videoid
?
- Related: How to use Servlets and Ajax? – BalusC Commented Aug 14, 2011 at 23:02
3 Answers
Reset to default 3There is no such content type as just "text" as far as I know, so it probably defaults back to XML.
Change the line to:
response.setContentType("text/plain");
The jquery documentation on get
says:
The success callback function is passed the returned data, which will be an XML root element, text string, JavaScript file, or JSON object, depending on the MIME type of the response. It is also passed the text status of the response.
This implies that the format of the data returned by the servlet depends on the HTTP Content-Type of your response. The one you are setting, "text", is not a valid MIME type. Thus jQuery won't recognize this format and will interpret it as an XML Document on the Javascript side. The correct MIME type for what you want is "text/plain".
Try
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
out.println("videoid");
out.close();
}
then you should receive "videoid" instead of an XML Document.
You should also hint at jQuery that you are receiving "text" rather than anything else in your response:
$.get("handleq", function(data, textStatus) {
alert("Done, with the following status: " + textStatus + "." +
" Here is the response: " + data);
}, "text");
By itself, text
is not a valid content type. I'd suggest you use text/html
instead:
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("videoid");
And specify that content type in the client-side call to $.get():
$.get("handleq", function(data, textStatus) {
alert("Done, with the following status: " + textStatus
+ ". Here is the response: " + data);
}, "html");
本文标签: javajqueryget and servletStack Overflow
版权声明:本文标题:java - jquery.get and servlet - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744208122a2595287.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论