我在HTML
中有一個表格,我使用JavaScript
獲取的值。我將其轉換爲對象爲以下內容(JavaScript
只):將JSON對象傳遞給Java類而不使用servlet
var obj = {
LogReference:logrefgenerator(),
ReferenceNumber : ""
}
現在,我想用Ajax
(或任何其他爲此事)這個對象發送到一個Java
類(NOT 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>
運行你的java代碼是什麼? – Hacketo
url:'ResourceServlet.java',是不妥當的,請你分享這個類 –
請檢查我的編輯@SaurabhJhunjhunwala – TheLuminor