$ .ajax does not respond

2

Code:

<script>
$(document).ready(function () {
    $("#submitbuy").click(function () {
        alert("clicou no submitbuy");
        var emailc = $("#emailc").val();
        alert("variavel emailc atribuida: " + emailc);
        $.ajax({
            url: 'https://meusite.com/cadastro.php',
            type: 'POST',
            data: $("#forms").serialize(),
            success: function () {
                alert("entrou no success ajax");
                if (data.length == 0) {
                    alert("forms foi submit");
                    $("#forms").submit();
                } // if
                else {
                    alert("forms não foi submit, alert data");
                    alert(data);
                } // else
            } // success
        }); // ajax
    }); // submitbuy.click
}); // document.ready
</script>

What happens: Displays the first two alerts, (I used them as debug) ps: in firebug does not point any error in this code above. But I did not display the third alert (which is inside the $ .ajax success).

What will be the problem? I have another ajax code on this same page, and it fulfills its role, I've scanned all possible errors, but nothing found ..

EDIT: Code form, to leave something more concrete: Form code is like this, it's very simple, just to capture the email:

<form action="https://www.meusite.com/paginax" method="POST" id="forms" name="forms">
   <input name="emailc" id="emailc" placeholder="seu melhor email!" id="emailc" type="text">
</form>

The input is outside the form, type button, only to open the function for later submission in AJAX.

    
asked by anonymous 03.01.2015 / 16:23

1 answer

1

From code comments, I come to assume that you're using something like

<form>
    <input type="submit" id="submitbuy">
</form>

Problems may occur:

  • By clicking the button the page may be refreshing and this aborts the ajax request.
  • If you are not using "submit" button, there may be a problem with ajax, so you should use the callback error:
  • (As the author of the question found @ AlexandreC.Caus) You are probably using the entire url, when only the PATH would be the most recommended. It can be used like this:% with% absolute path. Or relative path: url: '/cadastro.php' The advantage is that if the domain name changes its code it will still work without making modifications, it also helps with differences between url: 'cadastro.php' and https
  • As @bfavaretto reported, the date argument has not been set
  • To try to skip both issues, try something like:

    $(document).ready(function () {
        $("#submitbuy").click(function () {
            alert("clicou no submitbuy");
            var emailc = $("#emailc").val();
            alert("variavel emailc atribuida: " + emailc);
            $.ajax({
                url: 'cadastro.php',
                type: 'POST',
                data: $("#forms").serialize(),
                success: function (data) {//Adicionado o argumento data
                    alert("entrou no success ajax");
                    if (data.length == 0) {
                        alert("forms foi submit");
                        $("#forms").submit();
                    }
                    else {
                        alert("forms não foi submit, alert data");
                        alert(data);
                    }
                },
                error: function (err) {//Detecta erros na requisição
                    alert(err);
                }
            });
            return false;//Bloqueia redirecionar em caso de type=submit
        });
    });
    

    The http (or error: ) is required, because if there is a problem communicating with the server or the internet (ISP), only it can report this to you.

    Please note that the most current version of ajax is encouraging Deferred , this does not have to be with your problem is only a hint, so if you want you can start using it like this:

    $.ajax({
        url: 'https://meusite.com/cadastro.php',
        type: 'POST',
        data: $("#forms").serialize()
    }).done(function (data) {//Equivalente ao success (Adicionado o argumento data)
        alert("entrou no success ajax");
        if (data.length == 0) {
            alert("forms foi submit");
            $("#forms").submit();
        } else {
            alert("forms não foi submit, alert data");
            alert(data);
        }
    }).fail(function (err) {//Equivalente ao error
        alert(err);
    });
    
        
    03.01.2015 / 16:36