AJAX POST request to PHP API returns HTML security challenge instead of JSON

Website URL

http://pglifeproject.freepage.cc/

Error Message

When I click the Book Now button on my website, I get this error:

“Server returned an invalid response. Check Console.”

My JavaScript sends a POST request using fetch() to /api/book.php. The request returns HTTP 200 OK, but the response is HTML instead of JSON.

The HTML response contains /aes.js, sets a __test cookie, and redirects to /api/book.php?i=1.

My PHP endpoint uses json_encode() to return JSON. The website loads normally, and login, signup, and the Interested button work. Only Book Now is failing.

Could you please help me resolve this security verification issue for legitimate AJAX POST requests?

Other Information

Additional Information:

I checked the request in Chrome DevTools → Network → Fetch/XHR.

Request URL: http://pglifeproject.freepage.cc/api/book.php

Request Method: POST

Status Code: 200 OK

Response Content-Type: text/html

The response contains an HTML security challenge that loads /aes.js, sets a __test cookie, and redirects to /api/book.php?i=1 instead of returning JSON.

My booking.js uses fetch() with FormData to send the property_id. The PHP endpoint checks the login session, validates the property, checks for an existing booking, and inserts the booking into MySQL.

The Interested button works, but Book Now fails because the response cannot be parsed as JSON.

I would appreciate guidance on resolving this issue on InfinityFree hosting.

Welcome to the InfinityFree community forum! Good to have you here.

  • Most people answering here are volunteers. They’re often the fastest source of help. Admin (the only staff member) checks in about once a day for issues on our end.
  • Volunteers can’t see your account or website. Share what you actually see, like error messages and what happens when you try something. That’s more useful than your theory of what’s wrong.
  • AI tools often get InfinityFree wrong. If a volunteer’s advice differs from what an AI told you, please try the volunteer’s suggestion first.
  • Solving an issue is a joint effort. Try the suggestions you get and report back what happened.

Beep boop, I’m a bot! I dug through the forum to get you started. I sometimes get things wrong though, so do with this what you will until a human has a look.

The HTML you’re getting is InfinityFree’s browser security check. It sets the __test cookie and redirects to ?i=1. It can’t be turned off on free hosting, but it shouldn’t normally trigger for AJAX calls to your own domain. The article on it says same-domain fetch() calls to your own endpoints work fine:

So the question is why your Book Now request reached the server without a valid __test cookie. Two things would narrow it down:

  1. Does it fail every time, or only sometimes, for example after the tab has been open for a few minutes? Another poster saw AJAX calls get the ?i= redirect after a few minutes of inactivity. It might be the same thing, but I can’t tell from your post. Their topic is here:

    Issue with URL Parameter Changes and AJAX Requests After Inactivity

  2. What exact URL does booking.js pass to fetch(), and what address is in the browser bar when you click Book Now? If the page is on one hostname and the request goes to another (for example a different subdomain or an absolute URL), the browser treats it as a cross-site request. The cookie wouldn’t be sent, and that would explain the challenge. The article lists cross-domain AJAX as something that doesn’t work.

You said the Interested button works. If it calls an endpoint the same way Book Now does, compare how the two fetch() calls differ, such as the URL and the credentials option. That would show whether the problem is in the request or somewhere else.

Thank you for your reply.

The issue happens every time I click the Book Now button. The website loads normally, and the Interested button works correctly.

The Book Now request receives HTTP 200, but the response is an HTML security challenge containing /aes.js and a redirect to /api/book.php?i=1 instead of the expected JSON response.

Could you please advise how to resolve this?

The AI is single time reply unfortunately so it won’t do a follow up…

Either way, it does seem that this is a coding issue rather than a hosting issue. The “Book Now” button works fine for me though, it has successfully made a POST request to the endpoint and returned the “Please login first” message.

Yes, same here.

fetch() (and other AJAX/XHR) requests within the same website should work fine, since the cookie is shared.

The only catch is that the cookie can expire. So if the page is open for a long time, the current cookie is no longer valid and the browser needs a new one to continue. But then you should be able to refresh the page and continue from there, and the issue shouldn’t be happening all the time.

Hello, thank you for the guidance.

I refreshed the property detail page using Ctrl+F5 and logged in again, but the issue persists.

When I click Book Now, the POST request to /api/book.php still returns the HTML security challenge containing /aes.js and redirects to /api/book.php?i=1, instead of returning JSON.

Opening /api/book.php directly in the browser returns the expected JSON message Invalid request method., so the PHP endpoint is executing normally for a GET request.

Could you please suggest what else I should check? The issue persists even after a fresh page reload and login.

The API call worked fine for both @Meishin and myself, and the issue you’re describing does not correspond to any platform issue that I recognize.

But thee are some things we can determine.

Firstly, do you know if the Javascript code that calls the /api/book.php endpoint works differently for logged in vs. not-logged in users? @Meishin and I only got the “Please login first” response since we didn’t create accounts. You say you were logged in, and if that puts you on a different page with different Javascript code, that could explain the difference in behavior.

Secondly, we can check the actual cookie that was sent.

In the dev tools in your browser, open the Network tab and then trigger the API request to the book endpoint. Then click the request, and go to the Cookies tab. Can you check that:

  • The __test cookie is present at all?
  • The __test cookie value matches the one under Application → Cookies?
  • If you then refresh the page, does it add ?i=1 to the page URL?