我创建了一个AJAX Web应用程序,它应该向我的servlet发送一些数据(POSITIONS
),这将在我的服务器中创建包含该数据的文本文件。问题是我不知道它是否有效,我不知道web.xml
如何工作以及如何为我的应用配置它。
< / p>
更新:
我收到了CORS问题,但我的webapp.html和我的.war文件都在我的本地主机上。
服务器:Apache Tomcat 7.0
Eclipse EE Mars
提前谢谢。
PS:我在下面发布的片段只是为了观察,但我不能使用脚本,因为我不拥有图书馆所在的业务,如果你想看到它在上面的链接上查看它这个PostScript。
我的AJAX
$(document).ready(function() {
var POSITIONS;
//var data is a dynamic JSON file that should be created in the backend.
var data = [{
label: 'node1',
id: 1,
children: [{
label: 'child1',
id: 2
}, {
label: 'child2',
id: 3
}]
}, {
label: 'node2',
id: 4,
children: [{
label: 'child3',
id: 5
}]
}];
$('#tree1').tree({
data: data,
autoOpen: true,
dragAndDrop: true
});
console.log($('#tree1').tree('toJson')); //This will give you the loading jqtree structure.
$('#tree1').bind(
'tree.move',
function(event) {
event.preventDefault();
// do the move first, and _then_ POST back.
event.move_info.do_move();
console.log($(this).tree('toJson')); //this will give you the latest tree.
POSITIONS = $(this).tree('toJson');
alert(POSITIONS);
$.post('http://sistema.agrosys.com.br/sistema/labs/CSS_HTML/', {
tree: $(this).tree('toJson')
});
alert("done"); //this will post the json of the latest tree structure.
}
);
var data = new FormData();
data.append("JqTree", POSITIONS);
alert('Sending: ' + POSITIONS);
$.ajax({
url: '/JqTree/Hello',
type: 'POST',
data: data,
cache: false,
dataType: 'json',
processData: false,
contentType: false,
success: function(response) {
alert("file has been successfully sent\n\n" + POSITIONS);
},
error: function(jqXHR, textStatus, errorThrown) {
alert('ERRORS: ' + textStatus);
}
});
});
我的Servlet
import java.io.IOException;
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 Hello extends HttpServlet {
private static final long serialVersionUID = 1L;
public Hello() {}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.print("<html><body>");
out.print("<h3>Hello Servlet</h3>");
out.print("</body></html>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String position = request.getParameter("JqTree");
PrintWriter writer = new PrintWriter("Positions.txt", "UTF-8");
writer.println(position);
writer.close();
}
}
和我的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>JqTree</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>Hello</display-name>
<servlet-name>Hello</servlet-name>
<servlet-class>Hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>
</web-app>
答案 0 :(得分:1)
根据屏幕截图,您通过file://
网址打开了HTML页面。当然,您将点击CORS墙,因为ajax请求不会在file://
网址上触发,而是在http://localhost
网址上触发。
修复file://
网址也是一个值得信赖的http://localhost
网址,绝不会将file://
网址用于网络资源。将source.html
文件移动到项目的公共Web内容中,然后通过以下方式打开它: