How do I debug jquery AJAX calls?

后端 未结 10 1324
悲&欢浪女
悲&欢浪女 2020-11-29 05:19

I have been working on trying to get AJAX to work with Jquery. My big issue so far has been that I don\'t really know how to figure out where I\'m making a mistake. I don\'t

相关标签:
10条回答
  • 2020-11-29 05:31

    Firebug as Firefox (FF) extension is currently (as per 01/2017) the only way to debug direct javascript responses from AJAX calls. Unfortunately, it's development is discontinued. And since Firefox 50, the Firebug also cannot be installed anymore due to compatability issues :-( They kind of emulated FF developer tools UI to recall Firebug UI in some way.

    Unfortunatelly, FF native developer tools are not able to debug javascript returned directly by AJAX calls. Same applies to Chrome devel. tools.

    I must have disabled upgrades of FF due to this issue, since I badly need to debug JS from XHR calls on current project. So not good news here - let's hope the feature to debug direct JS responses will be incorporated into FF/Chrome developer tools soon.

    0 讨论(0)
  • 2020-11-29 05:31

    Just add this after jQuery loads and before your code.

    $(window).ajaxComplete(function () {console.log('Ajax Complete'); });
    $(window).ajaxError(function (data, textStatus, jqXHR) {console.log('Ajax Error');
        console.log('data: ' + data);
        console.log('textStatus: ' + textStatus);
            console.log('jqXHR: ' + jqXHR); });
    $(window).ajaxSend(function () {console.log('Ajax Send'); });
    $(window).ajaxStart(function () {console.log('Ajax Start'); });
    $(window).ajaxStop(function () {console.log('Ajax Stop'); });
    $(window).ajaxSuccess(function () {console.log('Ajax Success'); });
    
    0 讨论(0)
  • 2020-11-29 05:34

    here is what I would do in order to debug and get the php errors into javascript console.log after an ajax call:

        $('#ChangePermission').click(function () {
        $.ajax({
            url: 'change_permission.php',
            type: 'POST',
            data: {
                'user': document.GetElementById("user").value,
                'perm': document.GetElementById("perm").value
            },
            success: function (data) {
                console.log(data);  
            },
            error: function (data) {
                console.log(data);
            }
        });
    });
    

    on the php side I would start by asking for all error returned to string concat and echo as a json encoded response that will includes php error messages as well if any.

    <?php
    
    error_reporting(E_ALL);
    require_once(functions . php);
    $result = "true";
    $DBH = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);  
    $DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $STH = $DBH->prepare("INSERT INTO people ( username, permissions ) values (?, ?)");
    
    if (isset($_POST["user"]) && isset($_POST["perm"])) {
        $STH->bindParam(1, $_POST["user"], PDO::PARAM_STR);
        $STH->bindParam(2, $_POST["perm"], PDO::PARAM_STR);
    }
    try {
        $STH->execute();
    } catch (PDOException $e) {
        $result .= $e->getMessage;
    }
    echo json_encode($result);
    ?>
    
    0 讨论(0)
  • 2020-11-29 05:35

    Make your JQuery call more robust by adding success and error callbacks like this:

     $('#ChangePermission').click(function() {
         $.ajax({
             url: 'change_permission.php',
             type: 'POST',
             data: {
                 'user': document.GetElementById("user").value,
                 'perm': document.GetElementById("perm").value
             },
             success: function(result) { //we got the response
                 alert('Successfully called');
             },
             error: function(jqxhr, status, exception) {
                 alert('Exception:', exception);
             }
         })
     })
    
    0 讨论(0)
  • 2020-11-29 05:39

    Using pretty much any modern browser you need to learn the Network tab. See this SO post about How to debug AJAX calls.

    0 讨论(0)
  • 2020-11-29 05:40

    You can use the "Network" tab in the browser (shift+ctrl+i) or Firebug.
    But an even better solution - in my opinion - is in addition to use an external program such as Fiddler to monitor/catch the traffic between browser and server.

    0 讨论(0)
提交回复
热议问题