我在HTML
中有一个表单,我使用JavaScript
获取的值。我将其转换为对象,如下所示(仅在JavaScript
中):
var obj = {
LogReference:logrefgenerator(),
ReferenceNumber : ""
}
现在,我希望使用Ajax
(或其他任何其他内容)将此对象发送到我拥有的Java
类(NOT A SERVLET)。我试图这样做:
$.ajax({
url: 'Resource',
type: 'POST',
dataType :'json',
data: obj1,
success: function(result) {
alert('SUCCESS');
},
error: function(){
alert('Error');
}});
但上面的代码似乎并不起作用。 F12 debugger
(浏览器的调试工具)说:Error 404: Resource not found.
为什么这不起作用的任何建议?我希望从我的JavaScript
发送对象,并在我的Java代码中接收它以进行进一步处理。另请注意,我使用的是IE
。
修改:
以下是我的Resource.java
:
public class Resource extends HttpServlet {
private static final long serialVersionUID = 1L;
public Resource(String obj1) {
// TODO Auto-generated constructor stub
System.out.println(obj1);
System.out.println("inside resource!");
}
}
WEB.XML :
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="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_3_0.xsd" metadata-complete="true" version="3.0">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
</web-app>
答案 0 :(得分:1)
您的404表示服务器无法匹配请求URL处的资源。你说你不想使用Servlet但是试图POST一个。
您的网址应该是/ logHandler /。在您使用的任何Java Web框架中,您将把servlet(或其他一些处理程序)映射到该URL。你不能直接从客户端解决课程。