6

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 :/

j08691
  • 204,283
  • 31
  • 260
  • 272
Jordan
  • 1,433
  • 8
  • 14
  • Anything in the console? – Naftali Sep 10 '13 at 20:55
  • And in your php file? What's going on? – Guerra Sep 10 '13 at 20:55
  • what are the values of the other two parameters passed to the error callback? – Kevin B Sep 10 '13 at 20:56
  • Did you check you console what error does it give you. – Irfan TahirKheli Sep 10 '13 at 20:56
  • FYI, per [jQuery.ajax docs](http://api.jquery.com/jQuery.ajax/), "the .fail() method replaces the deprecated .error() method." – Phil Nicholas Sep 10 '13 at 20:56
  • can u put console.log(xhr.status); into the error function? – dscdsc Sep 10 '13 at 20:56
  • @Neal, nothing in the console at all – Jordan Sep 10 '13 at 20:56
  • Are you missing datatype? – DevelopmentIsMyPassion Sep 10 '13 at 20:56
  • @Jordan in the network tab? – Naftali Sep 10 '13 at 20:56
  • @Neal "nothing goes to console or the network tab" – j08691 Sep 10 '13 at 20:56
  • 1
    @PhilNicholas he isn't even using `.error` – Kevin B Sep 10 '13 at 20:56
  • @dscdsc It xhr.status gives me 0 – Jordan Sep 10 '13 at 20:57
  • @j08691 ahhh did not see that for some reason. – Naftali Sep 10 '13 at 20:57
  • Remove `data: '',` and try with `type: "GET"`. You are not passing anything, does not need to be post nor have the data attribute. – RaphaelDDL Sep 10 '13 at 20:57
  • I see you're doing a POST. If you call the url via browser you do a GET. Be sure if your php script is correct with a POST – Luca Rainone Sep 10 '13 at 20:58
  • Please clarify a few things. Are you getting to the error callback? What are the values of the three parameters passed to the error callback? (should be an object, a statuscode, and a jQuery error message). So far it sounds like either a server error or a pathing issue. there's nothing wrong with the jQuery code you have provided. – Kevin B Sep 10 '13 at 20:59
  • Lot of comments O.o, trying to keep up, I removed the `data: ''` line, and I am 99% certain my php is fine with a post (considering I don't even actually post any parameters for it to use), as this has been working the last few weeks. – Jordan Sep 10 '13 at 21:01
  • @KevinB Hey Kevin, yes, I am getting to the error callback, and aren't the statuscode and error message the same as `xhr.status` and `xhr.responseText`? If so, they are `0` and `""` respectively. I agree with the pathing issue, I just don't see how that is possible when it works fine with the other file, which is located in the same place (PHP folder) – Jordan Sep 10 '13 at 21:04
  • the status yes, but not the jquery error text, though it might end up being "" if jQuery can't figure out what the problem is. – Kevin B Sep 10 '13 at 21:05
  • Status code 0 mean no network connectivity generally. Can you look into that, i know its the least probably thing. – Arpit Singh Sep 10 '13 at 21:06
  • @chumkiu Bizarrely, changing it to a GET actually makes it work, even though it has been working with POST for a few weeks. Any idea why changing file structure would cause this? – Jordan Sep 10 '13 at 21:06
  • Do you use an .htaccess file? If so can you post it? – j08691 Sep 10 '13 at 21:07
  • 1
    Try using error: function(xhr, textStatus, errorThrown) { ... } – Nick Bray Sep 10 '13 at 21:08
  • either firewall or htaccess file needs to be checked, it could be worthwhile moving the file to another folder and check again – Arpit Singh Sep 10 '13 at 21:09
  • Well, I don't know what to tell you guys. I changed it to a GET to see if that worked as chumkiu suggested, which worked. Then, I changed it back to a POST to check the error textStatus and errorThrown, and... it worked. Changed nothing else, just POST, to GET, and back to POST O.o – Jordan Sep 10 '13 at 21:11
  • @Jordan totally depends of your php file. Maybe there is some line that break something on POST – Luca Rainone Sep 10 '13 at 21:12
  • Boooooo, what a boring solution. Such a letdown ;) – j08691 Sep 10 '13 at 21:13
  • Sorry @j08691 ;) If it makes you feel better, I am a little letdown as well, was kind of hoping to figure out what actually was causing this. – Jordan Sep 10 '13 at 21:16
  • @Jordan Someone earlier in the comments suggested a status of 0 means network connectivity issues. That would completely explain why it continued to work - you just hit a blip in the network that resolved itself – Izkata Sep 10 '13 at 21:20
  • @Izkata I saw that, but wouldn't that have caused my other AJAX to fail? I first was attempting to fix this before lunch, then went out to lunch to stew on it, and the issue was still there (only for this ajax request) Seems like a network issue wouldn't make sense. Unless of course I am thinking about it incorrectly :P – Jordan Sep 10 '13 at 21:22
  • Please copy the AJAX BASE code I made and replace with your stuff and check what you are actually receiving: http://jsfiddle.net/RaphaelDDL/HAzbF/ (of course copy to your project, ajax will fail from jsfiddle for urls that don't return jsonp). – RaphaelDDL Sep 10 '13 at 21:49

1 Answers1

1

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

Community
  • 1
  • 1
mn.
  • 796
  • 6
  • 12
  • Both files are working now, but the one which was being a problem actually did not have any kind of json parsing, just database manipulations. Thank you for taking the time to give it some thought however! – Jordan Sep 10 '13 at 22:21