AJAX request gives error and status "canceled"

1

I have this code:

$("#go").click(function(){

    var email = $("#email").val();
    var password = $("#password").val();

    $.ajax({
        dataType:'html',
        type: 'post',
        data: 'email=' + email + '&password=' + password,
        url: 'login.php',
        success: function(data){
            alert(data);
        },
        error: function(data){
            alert("Error");
        }
    });
});

which generates the following request to the server:

Request URL:http://localhost/Snotes/login.php
Request Headers CAUTION: Provisional headers are shown.
Accept:text/html, */*; q=0.01
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Origin:http://localhost
Referer:http://localhost/Snotes/index.php
User-Agent:Mozilla/5.0 (Windows NT 6.3) AppleWebKit/537.36 (KHTML, like Gecko)     Chrome/35.0.1916.153 Safari/537.36
X-Requested-With:XMLHttpRequest
Form Dataview sourceview URL encoded
email:123
password:123

The request url is correct and the data also, but the error function is always called, in the chrome network tab it says that the status is 'canceled', can anyone help me with this error?     

asked by anonymous 09.07.2014 / 04:22

1 answer

5

Ok problem solved, I had to add a parameter to the click callback and use the .preventDefault() method in the event to prevent the default behavior that is submitting the form.

Source: link .

Thanks to all who helped;)

Solution code.

$("#go").click(function(e){

    e.preventDefault();

    var name = $("#name").val() + "%20" + $("#last_name").val();
    var email = $("#email").val();
    var password = $("#password").val();

    $.ajax({
        type: 'post',
        url:  'new.php',
        data: 'name=' + name + "&email=" + email + "&password=" + password,
        success: function(data){
            alert(data);
        },
        error: function(data){
            alert("Failed to send data to server!");
        }
    });

});
    
09.07.2014 / 06:25