开发者

Errors in jQuery ajax are not getting reported

开发者 https://www.devze.com 2023-03-31 13:42 出处:网络
I\'m having difficulty with 开发者_运维技巧the jQuery ajax success handler. Any javascript run-time errors that occur in the success handler dont seem to be getting reported (no errors appear in the F

I'm having difficulty with 开发者_运维技巧the jQuery ajax success handler. Any javascript run-time errors that occur in the success handler dont seem to be getting reported (no errors appear in the Firefox error console). And trying to debug without error notification is driving me crazy. Could someone take a look at the simplified version of my code below and let me know if I'm doing something stupid that might be causing the problem.

If not, if someone could test this in Firefox and confirm (or not) that no error messages appear and its not just me going mad (or there is something wrong with my Firefox installation or something). I've also put the code on the web - so you can just click below to test it ....

http://www.alisonstrachan.co.uk/tests/ajax2/ajax_test2.html

ajax_test2.html

<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.js"></script>
<script type="text/javascript">

$(document).ready(function(){

    //alert(missingVariable1);           //uncomment -> get error reported (as expected)

    $.ajax({
        url: "ajax_test2_process.php",
        data: {send: "hello"},
        async: false,
        success: function(data){
            $("#results").append(data);
            alert(missingVariable2);     //no error reported (there should be surely?)
            $("#results").append(" finished ");
            }
        });
});

</script>
</head>
<body>
<div id="results"></div>
</body>
</html>

ajax_test2_process.php

<?php
    echo "received: " . $_REQUEST['send'];
?>


My guess is that it's silently failing due to jQuery wrapping the callback in a try...catch.

So, put simply, any errors or bad code found within a callback function will just silently fail out (and bubble up to the try...catch I've outlined) then halt processing where it lies.


This might be due to Adblock Plus or a similar extension (check the extensions that you have installed), see bug 653533.


Take a look at the other call backs for the Ajax function (http://api.jquery.com/jQuery.ajax/) - you might want to take a look at error and also statusCode - Firebug is also very useful for investigating ajax related issues - you can see the post and response for an ajax communication.

If you want to log to the firebug console use console.log('text');


you could also try wrapping your ajax in an object like the jquery docs suggests:

var jqxhr = $.ajax({ url: "example.php" })
    .success(function() { alert("success"); })
    .error(function() { alert("error"); })
    .complete(function() { alert("complete"); });
0

精彩评论

暂无评论...
验证码 换一张
取 消