HTTP_ACCEPT Not Working on AJAX Called PHP

I’m sending AJAX calls to a php file and on the top of the PHP file I have

if ((isset($_SERVER['HTTP_ACCEPT']) === true) && (strstr($_SERVER['HTTP_ACCEPT'], 'image/webp') !== false)) {
//do something
}

That works on normal PHP pages, but fails on the PHP page being called by AJAX. What am I doing wrong?

all feedback appreciated!
Cheers!
Ryan

Hi Ryan,

I doubt that AJAX requests send “image/webp” in Accept header
Usually there are something like application/json, text/html, text/javascript.
What are you trying to achieve with that code?

The ajax delivers back content to be displayed, and i’m looking for the image/webp in the accept header to see if I should return content with webp images or jpg images.

I guess on the front end I could do a string replace on the returned ajax data, swapping all “.jpg” with “.webp” or the other way around.

Right now my solution is that I make a cookie if webp in accept header upon opening the page, and then check for the cookie within the AJAX PHP file being called for more content.

Cheers!
Ryan

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.