Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

A jQuery $.ajax call to a PHP file returns 404 Not Found

ErrorjqXHR.status is 404 and the error callback gets errorThrown set to "Not Found"

Short answer

A 404 means the server found nothing at the exact address the browser requested, so the PHP file is usually fine and the address is wrong. Print the full URL the browser built, then request it directly. The usual causes are a relative path resolved from the page and not from the script, a base tag, or an application in a subfolder with a root-relative path.

  • Applies to jQuery 3, PHP 8.3, Apache 2.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Work out the full address the browser really requests, then ask the server for exactly that. Resolve your url value against the current page and compare it with the real file location.

    // in the browser console, on the page that sends the request
    console.log(new URL('ajax/save.php', window.location.href).href);
  2. 2Match what you see, then jump to its fix

    1. 1The printed address contains the page's folder, such as /admin/users/ajax/save.php, and the server has no file thereA relative URL is resolved from the page, not from the scriptFix
    2. 2The printed address uses a different folder than the page's own URLA base tag changes what relative meansFix
    3. 3The site opens at http://localhost/myapp/ and the request goes to http://localhost/ajax/save.phpThe app lives in a subfolder, but the path starts at the site rootFix
    4. 4The direct curl request for the printed address also returns 404The file name or folder in the url is not the one on diskFix

What does the error look like?

  • jqXHR.status is 404 and the error callback gets errorThrown set to "Not Found"
  • The error callback runs instead of the success callback, because the HTTP status is 404

Cause 1 of 4

A relative URL is resolved from the page, not from the script

1Check

A relative url is not resolved against the folder of the .js file. The browser resolves it against the page that runs the script (the XHR standard parses the URL relative to the relevant settings object). The same script then works on one page and fails on a page in a deeper folder.

curl -I http://localhost/admin/users/ajax/save.php
curl -I http://localhost/ajax/save.php

2Fix

Use a path that starts with a slash, so it no longer depends on which page loads the script.

$.ajax({
  url: '/ajax/save.php',
  method: 'POST',
  data: $('#profile').serialize()
});

3Verify

Open the page again, trigger the request and watch the status in the Network panel. The response should be 200, or whatever your script returns.

curl -I http://localhost/ajax/save.php

Confirmed by xhr.spec.whatwg.org

Did this fix it?

Cause 2 of 4

A base tag changes what relative means

1Check

The base element sets the base URL for all relative URLs in the document, so a relative url in your script follows it. A template or framework layout may add one.

grep -rn "<base" .

2Fix

Either remove the base tag or write the AJAX url as a full path from the site root.

$.ajax({ url: '/ajax/save.php', method: 'POST', data: formData });

3Verify

Print the address again. It should match the real file location.

console.log(new URL('/ajax/save.php', window.location.href).href);

Confirmed by developer.mozilla.org

Did this fix it?

Cause 3 of 4

The app lives in a subfolder, but the path starts at the site root

1Check

A path that starts with a slash is resolved from the site root. Apache appends the URL path to the DocumentRoot, so /ajax/save.php means DocumentRoot plus /ajax/save.php, and the file sits in /myapp/ajax/ instead.

curl -I http://localhost/ajax/save.php
curl -I http://localhost/myapp/ajax/save.php

2Fix

Put the base path in one place and build every AJAX url from it. Let PHP print it into the page so development and production can differ.

<script>
  const BASE_URL = <?= json_encode('/myapp') ?>;
</script>

// later, in your script
$.ajax({ url: BASE_URL + '/ajax/save.php', method: 'POST', data: formData });

3Verify

Request the endpoint through the page and then with curl.

curl -I http://localhost/myapp/ajax/save.php

Confirmed by httpd.apache.org

Did this fix it?

Cause 4 of 4

The file name or folder in the url is not the one on disk

1Check

A 404 only says the server cannot find the resource. A mistyped url, or a file that was moved or renamed, produces the same status. Compare the printed address with the real path under the document root.

curl -I http://localhost/ajax/save.php
find /var/www/html -name "save.php"

2Fix

Correct the url to the real path, or move the file to where the url points.

$.ajax({ url: '/api/save.php', method: 'POST', data: formData });

3Verify

The direct request now returns 200 and the AJAX call succeeds.

curl -I http://localhost/api/save.php

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Keep every endpoint url in one place, a base-path constant, and never type paths inline.
  • Use paths from the site root, or a base constant, instead of paths relative to the page.
  • Log jqXHR.status and the requested url in a shared .fail() handler, so a 404 names the address that failed.
  • Test one page in a deeper folder, because relative paths often pass on the home page and fail there.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.