hulkatron
hulkatron

Reputation: 185

Not getting alert on success in AJAX

This is my AJAX code. It's working fine and sends data to my database. However I don't get any alert() on success.

Can you tell me why?

$(function() {    
    $('#placeBid').on('submit', function(e) {
        $.ajaxSetup({
            headers: {
                'X-CSRF-Token': $('meta[name="csrf_token"]').attr('content')
            }
        });
        e.preventDefault(e);
        $.ajax({
            method:"POST",
            url: $("#placeBid").attr("action"),
            data: $(this).serialize(),
            dataType: 'json',
            success: function(data) {
                if (data == true)
                    alert('working');
                else 
                    alert('not working');            
            },
            error: function(data) {}
        })
    });
});

Upvotes: 1

Views: 1640

Answers (1)

Zakaria Acharki
Zakaria Acharki

Reputation: 67505

Remove the dataType: 'json' option will fix the issue because the callback will wait for data to be json when you're returning I guess string because you're trying to make a condition data == true (by the way it will be never achieved because data returned could not be boolean).

So just remove the option and dataType will make by default Intelligent Guess for (xml, json, script, or html) :

$.ajax({
    method:"POST",
    url: $("#placeBid").attr("action"),
    data: $(this).serialize(),
    success: function(data) {
      if (data == 'true')
        alert('working');
      else 
        alert('not working');            
    },
    error: function(data) {}
})

Hopet this helps.

Upvotes: 1

Related Questions