AJAX fails

I have a problem with one of my AJAX requests. It all worked before, then I moved a few files in folders and it must have broken something, but I couldn’t figure out what might be wrong in my path.

What's happening

It seems that the AJAX request is being ignored, nothing is happening on the console or on the network tab, responseText is "", and I put a breakpoint on the first line of PHP that never hits. It goes straight from $.ajax to the first line in my callback. Also, when this PHP code gets called directly to a page or cron, it works fine.

I have a very similar call later to another file in the same folder, which works fine.

What i tried

As mentioned earlier, I looked at responseText, console output, network tab and breakpoints in my PHP (which were never reached), and I looked at Google and stackoverflow. If I intentionally change the name to a nonexistent file, I quickly get an error message 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) captures the code from the 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 simple AJAX calls, not sure what is going on: /

+6
source share
1 answer

The error callback is caused by http errors, but also if the JSON parsing response does not work

From: JQuery ajax error callback

From your working file, the dataType: 'json' im code blindly assumes you are doing some kind of json parsing. Try just making "myFile.php" a simple echo 'x'; script and check the response text. If you get something, it means that your script contains JSON parsing code, but you are not passing any data to the script in any case.

Consequently, an error callback will be called because the script is trying to JSON not parse anything (i.e. data: '' )

I cannot offer anything else, from reading your description of the questions, I hope someone else finds out. Good luck

+1
source

Source: https://habr.com/ru/post/953589/


All Articles