有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

javascript不使用servlet将JSON对象传递给Java类

我在HTML中有一个表单,我使用JavaScript获取它的值。我正在将其转换为对象,如下所示(仅在JavaScript):

var obj = {
            LogReference:logrefgenerator(),
            ReferenceNumber : ""
}

现在,我希望使用Ajax(或任何其他相关内容)将此对象发送到我拥有的Java(不是SERVLET)。我试图通过以下方式做到这一点:

    $.ajax({
    url: 'Resource',
    type: 'POST',                                                      
    dataType :'json',
    data: obj1,
    success: function(result) {
        alert('SUCCESS');
    },
    error: function(){
        alert('Error');
}});

但是上面的代码似乎不起作用。F12debugger(浏览器的调试工具)说:Error 404: Resource not found.

有什么建议说明为什么这行不通吗?我希望从我的JavaScript发送对象,并在我的Java代码中接收它,以便进一步处理。另外,请注意,我使用的是IE

编辑: 以下是我的{}:

    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!");
    }
}

网络。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>

共 (1) 个答案

  1. # 1 楼答案

    404表示服务器无法匹配请求URL处的资源。您说您不希望使用Servlet,但正在尝试发布到Servlet

    您的URL应该类似于/logHandler/。在您使用的任何JavaWeb框架中,您都会将servlet(或其他处理程序)映射到该URL。基本上不能直接从客户机寻址类