我有一个AJAX对servlet进行POST。 Servlet进行一些计算并返回响应。 AJAX成功函数读取响应并执行某些操作。
AJAX CALL
$.ajax( {
type: "POST",
url: "/bin/path/to/Servlet",
data: $(this).serialize(),
dataType: "html",
success: function(responseValue) {
if(responseValue == '200') {
// Do something
}else {
console.log("it is not 200");
}
},
error: trialForm.trialError
}).done(function(status) {
$(trialForm.submitButton).show();
$(trialForm.loader).hide();
});
}
SERVLET
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) {
response.setContentType("text/html");
URL url = new URL("www.apriurl.com");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type","application/json");
conn.setRequestProperty("Authorization","XXXXZZZZ " + strSig);
conn.setRequestProperty("Accept","application/json");
conn.setRequestProperty("ZZZZZ",clientID);
OutputStream os = conn.getOutputStream();
os.write(inputParameters.getBytes());
os.flush();
System.out.println(conn.getResponseCode());
response.getWriter().write(conn.getResponseCode());
}
答案 0 :(得分:1)
这是write (int)
而不是write (String)
所以,如果你write(200)
,它发送ascii值为200
尝试将200
作为字符串
作为
response.getWriter().write(String.valueOf (conn.getResponseCode()));
请参阅https://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html#write(int)
public void write(int c)
写一个字符。