I have a form in HTML
where I collect user input and store it as an object in JavaScript
. Here is how I am creating the object:
var dataObject = {
Name: getName(),
Age : getAge()
}
Now, I want to send this object using Ajax
to a backend Java
Class (not a servlet). Here's my attempt:
$.ajax({
url: 'ServerResource',
type: 'POST',
dataType :'json',
data: dataObject,
success: function(response) {
alert('Data sent successfully!');
},
error: function(){
alert('Error occurred while sending data');
}});
However, when I try to run this code, I encounter a 404 Error stating 'Resource not found'. Any insights on why this issue might be arising? My goal is to pass data from client-side JavaScript
to server-side Java
for further processing. Also, please note that I am testing this on Internet Explorer (IE)
.
Additional Info:
Here is a snippet of my ServerResource.java
:
public class ServerResource extends HttpServlet {
private static final long serialVersionUID = 1L;
public ServerResource(String dataObject) {
// TODO Auto-generated constructor stub
System.out.println(dataObject);
System.out.println("Inside Server 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>