1

I'm trying to use Ajax instead of the usual form post.

But what's the URL?? Should be my Java file, right? But I dont think I have access to the java files that's why I used the servlet mapping

url: 'ManageClients' DOESN´T WORK

Ajax

    $('#saveBtn').click(function() {
    $.ajax({
        type:'POST',
        url: '?????' ,
        success: function (result) {
            console.log(result);
        }
    });

ManageClients.java

public class ManageClients extends HttpServlet{

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    bla bla bla

   }

Web.xml

    <servlet>
    <servlet-name>Clients</servlet-name>
    <servlet-class>com.atp.servlets.Controller.ManageClients</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Clients</servlet-name>
    <url-pattern>/Clients</url-pattern>
</servlet-mapping>
Community
  • 1
  • 1

2 Answers2

0

Use url: 'Clients'

And use 'Data' parameter to send data for the ajax call. You could use '.serialize()' from jQuery to serialize your form and sent it as data.

$('#saveBtn').click(function() {
$.ajax({
    type:'POST',
    url: 'Clients',
    data: $("#myForm").serialize();
    success: function (result) {
        console.log(result);
    }
});

https://api.jquery.com/serialize/

Bruno Fortes
  • 1
  • 1
  • 1
0

URL Specifies which the request is sent to server side(Java Spring,ASP.net and etc) for database interaction or applying business logic and then return the result back to view page like your jsp. for more details about ajax calling and methods refer here

$.ajax({
            type  : "POST",
            url   : "url_name", //that define in your javacode
            data  : {  Data to be sent to the server. It is converted to a query string, if not already a string                        
                   },      

            error : function(){
                        //error messoge log will displayed here
                    },          
    });
Lee
  • 77
  • 11