A jQuery $.ajax call to a PHP file returns 404 Not Found
jqXHR.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.
Fix it fast
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);2Match what you see, then jump to its fix
- 1
The 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
The printed address uses a different folder than the page's own URLA base tag changes what relative meansFix - 3
The 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
The direct curl request for the printed address also returns 404The file name or folder in the url is not the one on diskFix
- 1
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.php2Fix
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.phpConfirmed by xhr.spec.whatwg.org
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
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.php2Fix
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.phpConfirmed by httpd.apache.org
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.phpConfirmed by developer.mozilla.org
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
$_FILES key is undefined when a jQuery Ajax form posts files
Notice: Undefined index: file
With jQuery, a multipart upload needs a FormData body plus processData: false and contentType: false.
4 causes, each with a fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fix