Problem with AJAX Request

Asked

Viewed 40 times

1

I’m doing a user login system. When the User makes the form Ubmit the AJAX request is not working and the page switches to login.php

JS

$("form").submit(function() {

    if ($("#login_username").value() != "" && $("#login_password").value() != "") {

        $.ajax({
           url: $("#login-form").attr('action'), //login.php
           data: $("#login-form :input").serializeArray(),
           method: $("#login-form").attr('method'), //post
           success: function(data) {
               alert(data);
           }
        }); 
    }
    else {
        $("#text-login-msg").text("Digite o Usuário e senha").css('color', 'red');
    }
    return false;   
});

It seems that the Return false is not working

  • It seems to be javascript errors. I think the atr() must be attr()

  • even after correcting the errors of atr() keeps making the same mistakes

  • What error? vc need to turn on the error console in one’s browser F12 ai.

  • I managed to fix the error, it was some syntax error in IF

  • 1

    with jquery is val() in place of value()

1 answer

5


The return false; only works in Javascript inline in HTML.
For example (https://jsfiddle.net/Lho9vpe4/1/):

<form onsubmit="return enviar();">

To stop inside an Event Handler, which is your case, you have to use the .preventDefault();.

In your case it would look like this:

$("form").submit(function(e) {
    e.preventDefault();

However, if in no case will you send the form you could attach the event handset to a button or other element and call ajax, without having to send the form. Suggestion only.

  • 1

    what is "headphone"? The.o

  • 2

    @rray "Event Handler" -> http://portuguese.stackexchange.com/q/760/12

Browser other questions tagged

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