Servlet is responding but ajax does not reflect the answer

Asked

Viewed 118 times

0

Good morning,

I have a simple form in which I do Ubmit via ajax, then my Servlet takes this result and gives an answer, from the answer ajax will do something. I can see the answer in the browser, but I don’t get the result in ajax.

JSP:

`
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

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



<html>
  <head>
    <script src="http://localhost:8080/lib/js/jquery-3_2_1_min_js.js"></script>

  </head>
  <body>
    <form method="POST" ID="form1" >
        <%-- <%= request.getAttribute( "answer" ) --%>
        Name: <input type="text" name="name" /><br />
        E-mail: <input type="text" name="email" /><br />
        Address: <input type="text" name="address" /><br />
        <input type="submit" value="record" />
    </form>
<script>
$(document).ready(function(){
    $("#form1").on("submit", function(e){
        e.preventDefault();
        console.log("tried to submit the form");
        $.ajax({
            url: "/QualidadeWeb/testando",
            method: "POST",
            //async: false,
            data: $(this).serialize(),
            succes: function(data){
                console.log(data);
                console.log("it worked");
            },  
            error: function(data){
                console.log(data);
                console.log("didn't work");
            }
        });
    });
});


</script>
  </body>
</html>

`

Servlet:

`
package controllers;

import java.io.IOException;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class addContatoServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException{
        response.addHeader("Access-Control-Allow-Origin", "*");
        RequestDispatcher rd;
        rd = request.getRequestDispatcher("/WEB-INF/View/adiciona-contato.jsp");
        System.out.println("testando GET 444");
        try{
            rd.forward(request, response);
        }catch(Exception e) {
            e.printStackTrace();
        }   
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response)
            throws IOException, ServletException {
        response.addHeader("Access-Control-Allow-Origin", "*");
        System.out.println("Testando Post 1234");

        String name = request.getParameter("name");
        PrintWriter out = response.getWriter();

        response.setContentType("text/plain");
        response.setCharacterEncoding("UTF-8"); 
        out.write("the contact's name is: " + name);
    }
}

`

As you can see the answer works inserir a descrição da imagem aqui

However my ajax does not capture the answer in the console.log, it seems that ajax gets lost in the middle of the way:

inserir a descrição da imagem aqui

It only gets to the first console.log, does the post and then does nothing else, which could be?

Thank you in advance!

  • 1

    Are you sure e.preventDefault();that is correct?

1 answer

2


It seems the problem is a typo, in its code is written succes:

succes: function(data){

but it should be Success (ss):

success: function(data){

Remember to check out these details in the documentation.

  • You are correct, it was a typo even! Thank you very much!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.