3

I make an ajax call on an url that does exists, and does work, even when called with ajax.

Checking with firebug, the POST request fail due to a 404 error, but if I expand the data about the post request, in the response tab, I do find correctly the data that it is supposed to return.

So, everything is working (this is an login system, and users gets logged in), but the page still results as 404 error.

I don't have any clue about what can be causing this. I'm not sure either what code should I post, since this looks to me very strange and unexplainable. By the way, the same code seems to work on other pages. Anyone has some idea of what could possibly causing this kind of behaviour?

DavidTonarini
  • 941
  • 3
  • 17
  • 35
  • Please take a look at the following question which will be of use http://stackoverflow.com/questions/12922905/jquery-ajax-throws-404-for-an-existing-url?rq=1 – Nitin May 29 '14 at 12:24
  • Maybe, since it's a login page, after you post the data (I assume username/password) it redirects to a page that does not exist. So, the 404 is not from the login page but from the redirected page. – Uphill_ What '1 May 29 '14 at 12:33
  • Are you sure that it really does not return 404 if you open it in browser? Maybe you use ErrorDocument in .htaccess – Marek May 29 '14 at 12:34
  • I'm pretty sure it does exists. It loads if I copy the url and paste in the browser. What does concern me most is, however, that the correct data IS returned, even during the ajax call: firebug clearly shows that the response I get IS what it should be. – DavidTonarini May 29 '14 at 12:37
  • Do you set any headers in the page that you are posting to? – Derik Nel May 29 '14 at 12:46
  • Not that I'm aware of, but it might be that, since I am integrating a wordpress installation in my website, wordpress does? How can I check this, maybe from firebug? In the header section I don't see anything suspicious: what should I be looking for? – DavidTonarini May 29 '14 at 12:55
  • You could possibly check in the script that is being posted to if any php headers are set that could possibly result in your 404 Error. Could be due to some rules being triggered in that script that would result in a 404 Error. – Derik Nel May 29 '14 at 13:00
  • Just tried to check with headers_sent(); Nothing found :( – DavidTonarini May 29 '14 at 13:13
  • Actually that was the problem, although I still don't get why and how it came! I ADDED a header('HTTP/1.1 200 OK'), which overwrites whatever header was being returned (seemengly by wordpress). Now it works! I got the idea from here http://stackoverflow.com/questions/5760538/ajax-call-to-php-script-returns-404-error?rq=1 – DavidTonarini May 29 '14 at 13:19

2 Answers2

1

I still don't understand the problem, but I got it fixed. As Derik Nel suggested, the script might return some headers I wasn't aware of. That does seem to be the problem, although I can't figure out where and out, especially considering that headers_sent(); doesn't return anything

Yet, after I ADDED a header('HTTP/1.1 200 OK'), which overwrites whatever header was being returned (seemengly by wordpress), and now it works! I got the idea from here Ajax call to php script returns 404 error

Community
  • 1
  • 1
DavidTonarini
  • 941
  • 3
  • 17
  • 35
0

This can happen if you're doing a server-side redirect within the URL that you're calling via Ajax, and the URL that you're being redirected to is incorrectly formed, or doesn't exist.

If this is what's happening, the error you're seeing is likely the http error that's being thrown by the redirect. A better way to see what's going on is to check with a tool such as Fiddler and examine the actual http request. In the described scenario, you'll see 2 requests being made, one of which is the faulty redirect.

Chris Halcrow
  • 28,994
  • 18
  • 176
  • 206