1

I'm sure this will be easy for you guys but I'm struggling with this one. Just trying to do a simple ajax post request to a php file here.

I can see that the ajax request is working because I get valid data back from my php script (I verified this by echoing the result to the page). However, my ajax callbacks are not firing. See below:

Javascript:

    var request;
$(document).ready(function() {
    $('#loginform').submit(function(e) {

        if (request)
            request.abort();

        var $form = $(this);
        var $inputs = $form.find("input, select, button");
        $inputs.prop("disabled", true);

        var serializedData = $form.serialize();

        request = $.ajax({
            url: "/login.php",
            type: "post",
            data: serializedData,
            timeout: 10000,
            success: function() {
                alert("success");

             //   $("#result").html('Submitted successfully');
            },
            error: function() {
                alert("failure");
               // $("#result").html('There is error while submit');
            }
    });
});

PHP

<?php echo 'foo'; exit(); ?>

I almost feel stupid asking this considering the number of examples there are, but I cannot get this to work for the life of me. Suggestions?

EDIT Included html (Most of the css classes are from bootstrap btw)

<div style='background-color: #193048; width: 100%; height: 500px; background-repeat: no-repeat; background-position: center center; background-size: cover;'>
<div class="container">
    <div class="row">
        <div class="col-sm-6 col-md-4 col-md-offset-4">
            <h1 class="text-center login-title whiteFont">Sign in to continue</h1>
            <div class="account-wall">
                <div align='center'>
                    <img class="profile-img" align='center' src="https://lh5.googleusercontent.com/-b0-k99FZlyE/AAAAAAAAAAI/AAAAAAAAAAA/eu7opA4byxI/photo.jpg?sz=120"
                         alt="">
                </div>

                <form id="loginform" class="form-signin" method="post"  >          
                    <input type="text" name='emailTxt' class="form-control" placeholder="Email" required autofocus>
                    <input type="password" name="passTxt" class="form-control" placeholder="Password" required>    
                    <button class="btn btn-lg btn-primary btn-block" type="submit" value="login" id="loginBtn" >
                        Sign in</button>
                    <label class="checkbox pull-left">
                        <input type="checkbox" value="remember-me" >
                        <label class="whiteFont">Remember me</label>
                    </label>
                    <a href="#" class="pull-right need-help"><label class="whiteFont">Need Help?</label></a><span class="clearfix"></span>
                </form>
            </div>
        </div>
    </div>
</div>

2
  • Are you not getting the alert() firing? Have you checked your JavaScript console? Commented Dec 24, 2013 at 1:28
  • Correct, from the chrome javascript console, I see that the script executes with a 200 response. Commented Dec 24, 2013 at 1:30

2 Answers 2

2

.submit is most likely causing a postback, canceling out your JS. Add preventDefault to the beginning of your function:

e.preventDefault();
Sign up to request clarification or add additional context in comments.

3 Comments

This seems to stop execution of my javascript function all together? Now the postback to the php file isn't happening
The above statement wouldn't cause that to happen. Is anything happening in the console
I think you are correct. This appears to have solved it. Thank you!
2

You need to pass the data returned from the server as an argument to your callback

var request;
$(document).ready(function() {
  $('#loginform').submit(function(e) {

    if (request)
        request.abort();

    var $inputs = $form.find("input, select, button");
    $inputs.prop("disabled", true);

    var $form = $(this);
    var serializedData = $form.serialize();

    request = $.ajax({
        url: "/login.php",
        type: "post",
        data: serializedData,
        timeout: 10000,
        success: function(data, status, xhr) { // <---- changes made here
            alert("success");

         //   $("#result").html('Submitted successfully');
        },
        error: function() {
            alert("failure");
           // $("#result").html('There is error while submit');
        }
  });
});

5 Comments

Yes, you are correct. But I'm still not getting any events firing
Do you have the HTML you are working with. Want to get this running on my machine.
A test-case in jsfiddle would even be better.
Does jsFiddle support ajax requests like that? Where would the script go? I guess you could have a local script with access-control-origin.
@user2923779 included html.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.