Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AJAX failing silently

I am having a problem with one of my AJAX requests. All of this was working earlier, then I moved some files around in folders, and it must have broken something, but I have been unable to figure out what may be wrong with my pathing.

What is happening

AJAX request seems to be being ignored, nothing goes to console or the network tab, responseText is "", and I put a break point in the first line of the PHP which is never hit. It goes straight from $.ajax to the first line in my error callback. Additionally, when this PHP code is called by going straight to the page, or cron, it works fine.

I have a very similar call later to another file in the same folder that is working just fine.

What I have tried

As mentioned before, I have looked at responseText, console output, network tab, and breakpoints in my PHP (which are never reached), and I have looked around Google and stackoverflow. If I intentionally change the name to a non-existing file, I promptly get an error in the console.

Code below

        $.ajax({
        type: "POST",
        url: "PHP/myFile.php",
        data: '',
        success: function () {
            //Success code
        },
        error: function (xhr) {
            var response = xhr.responseText; // response is ""
            //Error code
        }
    });

Any ideas?

Here is the code, later in the file, which (successfully) grabs code from a file in the same folder.

$.ajax({
        type: "GET",
        cache: false,
        url: "PHP/myOtherFile.php",
        dataType: 'json',
        success: function (data) {
            // Success code
        },
        error: function () {
            // Error Code
        }
    });

Pretty basic AJAX calls, not sure what is going on :/

like image 555
Jordan Avatar asked Sep 10 '13 20:09

Jordan


1 Answers

Error callback is called on http errors, but also if JSON parsing on the response fails

From: Jquery ajax error callback

From your working file, the code dataType: 'json' im blindly assuming your doing some kind of json parsing. Try just making the "myFile.php" a simple echo 'x'; script, and check the response text. If your get something, then its because your script contains JSON parsing code, but your not passing any data to the script anyways.

Hence, the error callback would be called because the script is trying to JSON parse nothing (i.e. data: '')

I cant suggest anything else, from reading your questions description, hopefully someone else figures it out. goodluck

like image 133
Michael Nguyen Avatar answered Oct 22 '22 07:10

Michael Nguyen