Search code examples
phpjsonresponse

JSON output null in Firefox/Opera, correct in Chrome


Note: I'm not considering this is Drupal problem so I post it here on SO.

We created a Drupal module that is supposed to return JSON. E.g. calling /foo/json returns the JSON. This is all fine in Chrome. However, Firefox just shows "null".

The contents of the JSON is simply a PHP array with some information that gets populated with a loop

$someArray = array();
foreach(....) {
   $someArray[] = array("foo" => "bar", ...);
}

echo json_encode($someArray);

So far, I know that

  • it is not an encoding issue. If I only return a single element from $someArray[0]["some_key"], Chrome shows "USA" with a Content Length of 5, so I am sure that there is not a single non-ASCII character. However, Firefox shows null with a Content Length of 4.
  • doing a wget gives me the correct content with all the JSON. Since I trust wget more than the browser, I assume that it is not a Drupal/.htaccess issue.
  • The JSON is well-formed according to jsonlint.com and since the PHP function does the conversion, I assume it is really well-formed.
  • other stuff like permissions (everybody is allowed to acces the page) or encoding (sending UTF-8) doesn't change the result.
  • This is not jQuery/cross-domain related since I just want to call the URL in the browser and want to see the JSON response.
  • On another machine with the same (Drupal) set-up, the result is the same.
  • I am able to return JSON from other directories that are not related to the Drupal setup. But since I just do a json_encode, I bypass every possible output by Drupal and since wget works I it's not related to Drupal IMO.

updates according to comments

  • Content-type is application/json with proper encoding information. Changing it to text/html or something else doesn't change anything. Using both header as well as the Drupal function for setting headers.
  • I am sure that the null repsonse is correct, since I inspected it with Firebug.
  • ini_set('default_charset', 'UTF-8'); doesn't change anything since I already submit this information within the header.

Response header from Firefox with Firebug

Cache-Control   no-cache, must-revalidate, post-check=0, pre-check=0
Connection          Keep-Alive
Content-Language    en
Content-Length  4
Content-Type    application/json; charset=utf-8
Date            Mon, 03 Sep 2012 12:16:58 GMT
Etag            "1346674618"
Expires         Sun, 19 Nov 1978 05:00:00 GMT
Keep-Alive          timeout=5, max=100
Last-Modified   Mon, 03 Sep 2012 12:16:58 +0000
Server          Apache/2.2.22 (Ubuntu)
X-Powered-By    PHP/5.3.10-1ubuntu3.2

Request header

Accept          text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language en-us,en;q=0.7,de;q=0.3
Connection          keep-alive
Cookie          has_js=1; respimg_ratio=1; respimg=1000 //Drupal information
Host            vie.local
User-Agent          Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0

TL;DR
While Chrome shows correct (well-formed) JSON output, Firefox (and also tested in in Opera) only show null even for simplest string like "USA".


Solution

  • The problem was that Firefox, somehow, was not sending the correct language to the server. I don't know if via cookies or within the headers. Since I used language aware filtering for the output, the output was never returned.

    So disabling the language filter solved the problem. I have no idea why Firefox had issues with the language but this solved the problem.