avinash shah
avinash shah

Reputation: 1445

jQuery callback not called

Here is a portion of my script, it prints 9 then 98 but fails to print 2 it indicates that the callback function in jQuery is not called. However before this I am printing the json returned from the php file using json_decode function and json is printed absolutely fine. How can I go about debugging it, I mean where could be the error?

$(document).ready( function() {
alert(9);
$('#charac').keyup( function() {
alert(98);
  $.getJSON('myprg.php?q='+escape($('#charac').val()), function(data) {
    alert(2);

Upvotes: 1

Views: 2759

Answers (2)

Mario S
Mario S

Reputation: 11955

This may not be an answer, but the code is visualized better here.

Does the following also fail (using jQuery 1.5 or later)?

$(document).ready( function() {
    alert(9);
    $('#charac').keyup( function() {
        alert(98);
        var jqxhr = $.getJSON('myprg.php?q='+escape($('#charac').val()), function(data) {
            alert(2);
        })
        .success(function() { alert("second success"); })
        .error(function() { alert("error"); })
        .complete(function() { alert("complete"); });
    });
});

Or this:

$(document).ready( function() {
    alert(9);
    $('#charac').keyup( function() {
        alert(98);
        var jqxhr = $.getJSON('myprg.php?q='+escape($('#charac').val()), function(data) {
            alert(2);
        });

        jqxhr.success(function() { alert("second success"); });
        jqxhr.error(function() { alert("error"); });
        jqxhr.complete(function() { alert("complete"); });
    });
});

Upvotes: 1

mmarinero
mmarinero

Reputation: 365

Use the $.ajax function instead of getJSON and use the error callback to see what's going on.

$.ajax({
  url: url,
  dataType: 'json',
  data: data,
  success: callback
  error: callback(jqXHR, textStatus, errorThrown)
});

It can also be useful to inspect the actual server response with Firebug or Chrome developer tools and validate the JSON with JSONLint, some JSON libraries are more forgiving than others and ignore small errors.

Upvotes: 5

Related Questions