我想在tomcat中建立Jquery函数和servlet之间的通信。
Servlet代码:
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Test extends HttpServlet {
public static String getBody(HttpServletRequest request) throws IOException {
String body = null;
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = null;
try {
InputStream inputStream = request.getInputStream();
if (inputStream != null) {
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
char[] charBuffer = new char[128];
int bytesRead = -1;
while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
stringBuilder.append(charBuffer, 0, bytesRead);
}
} else {
stringBuilder.append("");
}
} catch (IOException ex) {
throw ex;
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException ex) {
throw ex;
}
}
}
body = stringBuilder.toString();
return body;
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
System.out.println(getBody(request));
out.println("Success Call Ajax POST");
}
public void doGet( HttpServletRequest request, HttpServletResponse
response ) throws ServletException, IOException{
response.setContentType("text/html");
response.setCharacterEncoding( "UTF-8" );
PrintWriter out = response.getWriter();
out.println("Get Method");
}
}
servlet标识在web.xml中定义
的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>Test</servlet-name>
<servlet-class>com.servlets.Test</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/toto</url-pattern>
</servlet-mapping>
</web-app>
以下HTML包含JQuery函数:
Jquery代码:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
function login(){
$.ajax({
type: "POST",
url: "http://localhost:8080/test/toto",
data: "POST Call",
success: function(result){
alert("success call"+result);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("Status: " + textStatus); alert("Error: " + errorThrown);
}
});
}
</script>
<title>My AJAX</title>
</head>
<body>
<button type="button" onclick="login()">Click Me!</button>
</body>
我用浏览器测试了servlet,没关系。当我尝试使用HTML / js应用程序并单击按钮时,我在eclipse控制台中显示了指令System.out.println(getBody(request))显示的消息; “POST Call”但我在浏览器中收到了错误警报。所以ajax函数成功调用了servlet中的方法post,但servlet无法成功返回对浏览器的响应。似乎servlet中存在问题。有人能帮帮我吗?
答案 0 :(得分:2)
在doPost()
尝试添加请求标头:
response.setContentType("text/html");
response.setHeader("Access-Control-Allow-Origin", "*");
我确信这与描述here
的Allow Origin问题有关答案 1 :(得分:0)
这是浏览器特定的。
它在Safari 8.0.3上工作正常,它会在Chrome和Firefox上抛出错误No 'Access-Control-Allow-Origin' header is present
。
在Firefox中查看控制台:
阻止跨源请求:同源策略禁止在http://localhost:8080/toto读取远程资源。这可以通过将资源移动到同一域或启用CORS来解决。
可以通过在浏览器中禁用web security
或已经提到过来修复它:
response.setHeader("Access-Control-Allow-Origin", your_domain);