Get AJAX message

6

Good afternoon,

I'm doing a User validation, but I can not receive the message I get. I have the following:

$(document).ready(function () {

  $("#UEmail").change(function () {
    $.ajax({
      type: "POST",
      url: "ValidarCorreo.jsp",
      data: {
             Correo: $("#UEmail").val()
             }
     })
       .done(function (msg) {
                                    
       if(msg==="true")
       {
         //Mostrar mensaje
        }
                                    
      });
     });
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
 <label class="col-sm-2 control-label">Correo Electronico:</label>
  <div class="col-sm-10">
   <div class="input-group m-b">
        <span class="input-group-addon">@</span>
         <input name="UEmail" id="UEmail" required type="text" class="form-control required email" placeholder="">
   </div>
   </div>
   </div>

the variable msg what it contains is the following HTML:

"↵↵↵↵↵↵↵↵↵↵↵↵↵↵↵<!DOCTYPE html>↵<html>↵    <head>↵        <meta http-
equiv="Content-Type" content="text/html; charset=UTF-8">↵        <title>JSP 
Page</title>↵        ↵    </head>↵    <body>↵        false↵    
</body>↵</html>↵"

This contains my JSP:

 <head>
       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
       <title>JSP Page</title>
       <%!
           String Correo;
       %>
</head>
<body>
    <%
      Correo=request.getParameter("Correo");

        if(Correo=="alguno@hotmail.com")
        {
            out.print("true");
        }
        else
        {
            out.print("false");
        }
    %>
</body>
    
asked by afar1793 16.01.2018 в 21:49
source

3 answers

1

You have to use the function submit in an input to send the data to your PHP or jsp file and create a to see the results that are sent by a post PHP example

<?php
$data = $_POST['UEmail'];
echo $data;
?>

code ajax:

$(document).ready(function () {

    $('body').on('submit', '#UEmail', function (e) {
        e.preventDefault();

        var DataString = new FormData($(this)[0]);
        var search = $("#UEmail").val();


 $.ajax({
                url: 'ValidarCorreo.jsp',
                type: 'POST',
                data: DataString,
                cache: false,
                contentType: false,
                processData: false,
                beforeSend: function () {

                $('#searcbtn').attr('disabled', 'disabled');
                },
                success: function (data) {
                    $("#resultados").html(data).show();

                } 
            });
    });
});
    
answered by 16.01.2018 в 22:51
1

The result of a ajax response is retrieved in the success property of the ajax .

Your ajax request should be like this:

$.ajax({
      type: "POST",
      //Esto define el tipo que dato que esperas recibir
      dataType:"text/html",
      url: "ValidarCorreo.jsp",
      data: {
         Correo: $("#UEmail").val()
      },
      success: function(response){
        //En response estaría el resultado que envías de tu jsp
      }
     })

UPDATE

The other big problem is how you are sending server data.

<head>
       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
       <title>JSP Page</title>
       <%!
           String Correo;
       %>
</head>
<body>
    <%
      //Declaramos el Printwriter, donde escribiremos nuestra respuesta
      que es lo que la llamada espera leer de vuelta
      PrintWriter writer = response.getWriter();
      //Le decimos que lo que vamos a devolver es de tipo texto (coincidendo 
      //con el declaro en la llamda ajax)
      response.setContentType("text/html");
      response.setCharacterEncoding("UTF-8");

      Correo=request.getParameter("Correo");

        if(Correo=="alguno@hotmail.com")
        {
            //Escribimos la respuesta en el buffer
            writer.println("true");
        }
        else
        {
            //Escribimos la respuesta en el buffer
            writer.println("false");
        }
        //Hacemos un flush para indicar que hemos termiando de escribir y 
        //queremos que se envie la respuesta
        writer.flush();
    %>
</body>

In any case, mention that it is not good practice to perform operations ajax to a jsp directly, you should make a call to Servlet and from there perform the necessary logical operations.

    
answered by 16.01.2018 в 22:31
0

Look these are sending your response wrong from the server what you have to do is send a valid format that you can convert from the client side with Javascript. And how do you do it? To do this on the server side you have to use a technology like JSON or XML depending on your taste, although I sincerely believe that JSON efficiency is better. Through the following Link , you can understand a little more about how to send them and treat them. I hope you help greetings.

    
answered by 17.01.2018 в 23:11