What’s wrong with my Ajax?

Asked

Viewed 108 times

0

My code has no return and I can’t figure out why.

index php.

<form id="login" method="POST">
        <input type="text" name="username" placeholder="Usuário ou email" />
        <input type="text" name="password" placeholder="Senha" />
        <button onclick="enviar()" type="submit">Entrar</button>
</form>
<script type="text/javascript">
    function enviar() {
        var formula = $('#login').serialize();

        $.ajax({
            type: 'POST',
            dateType: 'json',
            data: formula,  
            url: 'http://localhost/teste/enviar.php',
            success: function(data) {
                if (data == 1) {
                    alert("OK");
                    window.location = "";
                }

                if(data == 0) {
                    alert("Nada OK");
                    window.location = "";
                }
            },
            error: function(error) {
                console.log(error.responseText);
            }
        })
        return false;
    }
</script>
<script type="text/javascript" src="js/jquery-2.1.3.js"></script>

Send.php

<?php
$resposta = 1;
echo (json_encode($resposta));
  • ,what do you mean by "my function has no return?" the return would be to enter some "if", or return "false"? ,explain a little more of your problem

  • He should be warning 'OK', since I’m returning 1

  • I just tested it here and it worked perfectly, maybe the problem is in your "jQuery" file, you are sure it is in that location?

  • Have you seen the browser console? Does the order that jQuery loading and the lack of the $(document).ready can’t be the problem?

  • Try to remove the json_encode, let alone echo $resposta;. Only objects and arrays should be transformed to json.

  • Cancel the submitnormal, and call its function in the onSubmit thus: <form ..... onSubmit="enviar(); return false;" >

Show 1 more comment

1 answer

4

You are using "dateType", and the correct is "dataType".

Browser other questions tagged

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