[英]servlet - Why is the XMLHttpRequest responseText always blank?
I am losing my mind trying to solve this problem over here. 我失去了在这里解决这个问题的想法。 I have the following servlet deployed in Tomcat running on localhost:8080-:
我将以下servlet部署在localhost:8080-上运行的Tomcat中:
@WebServlet(urlPatterns = { "/createcon" }, asyncSupported = true)
public class CreateCon extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
ConcurrentHashMap<String, AsyncContext> map;
public CreateCon() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
public void init() {
map = new ConcurrentHashMap<>();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
AsyncContext context = request.startAsync(request,response);
context.setTimeout(10000);
if(!map.containsKey("Hello"))
map.put("Hello", context);
System.out.print("Inside GET");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
AsyncContext context = map.get("Hello");
PrintWriter writer = context.getResponse().getWriter();
writer.write(request.getParameter("message"));
writer.flush();
System.out.print(request.getParameter("message"));
}
}
As you can see I am trying to store an AsyncContext that is created in Map. 如您所见,我正在尝试存储在Map中创建的AsyncContext。 I code runs fine in Eclipse with Tomcat.
我的代码在Tomcat上的Eclipse中运行良好。 As you can see above that I have added
System.out.print
to actually check whether the code is working properly or not. 如您在上面看到的,我添加了
System.out.print
来实际检查代码是否正常工作。 And it works exactly as expected. 它的工作原理完全符合预期。
But the problem is with the javascript below-: 但是问题出在下面的javascript上:
function postMessage(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
alert(xmlhttp.responseText);
}
}
xmlhttp.open("POST", "/SampleTest/createcon", true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
var messageText = escape(document.getElementById("i1").value);
document.getElementById("i1").value = "";
xmlhttp.send("message="+messageText);
}
The onreadystatechange
fires exactly when expected but the xmlhttp.responseText
is always blank. onreadystatechange
完全在预期的时间触发,但是xmlhttp.responseText
始终为空。
I know that there is something known as a same-origin policy. 我知道有一种称为同源策略。 But I don't understand why that's a problem here ?
但是我不明白为什么这是个问题? I am running everything on
localhost:8080
. 我正在
localhost:8080
上运行所有内容。
Why is this still happening and how do I solve this ? 为什么这仍然会发生,我该如何解决?
Okay solved it. 好,解决了。 Guess it was my own silly mistake.
猜猜这是我自己的愚蠢错误。 The startchat() method should be modified like-:
startchat()方法应修改为:
function startChat() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "/SampleTest/createcon", true);
xmlhttp.send();
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
alert(xmlhttp.responseText);
}
}
}
Since I am trying to find the result from the request made at startChat()
. 由于我正在尝试从
startChat()
的请求中查找结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.