java - How to mention URL while submitting ajax form? -


i using html/jquery/ajax call servlet. submitting form servlet. gives 404 saying resource not found. have maven project.

i have servlet in src/main/java package : com.mycompany.sample

i have jsp in webapp/public folder.

in web.xml registered servlet using url pattern /mycontroller

in jsp gave action form below:

<form action="mycontroller" ............... 

and have separate .js file mentioned ajax call below"

 $.ajax({         type: "post",         url: "mycontroller",         data: datastring,         success: function(msg) {             alert('success');         },         error: function(ob,errstr) {             alert('onfailure');         }     }); 

but 404 error saying /mycontroller resource not found , onfailure alert.

am giving correct reference mycontroller in form , in ajax call?

thanks!

like other say, try full path because think cause of 404 missing application context.

i don't know if use taglibs can use url tag generate right url:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <c:url var="mycontrollervar" value="/mycontroller" /> 

then, put in ajax code:

$.ajax({     type: "post",     url: "${mycontrollervar}",     data: datastring,     success: function(msg) {         alert('success');     },     error: function(ob,errstr) {         alert('onfailure');     } }); 

else, can try full path because think application context missing. can retrieve following jsp var:

${pagecontext.request.contextpath} 

that that:

${pagecontext.request.contextpath}/mycontroller 

and ajax code:

$.ajax({     type: "post",     url: "${pagecontext.request.contextpath}/mycontroller",     data: datastring,     success: function(msg) {         alert('success');     },     error: function(ob,errstr) {         alert('onfailure');     } }); 

Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -