JSON output in Firefox / Opera, fixed in Chrome

Note. I do not address this issue with Drupal, so I post it here on SO.

We created a Drupal module that should return JSON. For instance. call / foo / json returns JSON. Everything is fine in Chrome. However, Firefox just shows "null".

JSON content is just a PHP array with some information that is populated by a loop

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

I still know that

  • this is not an encoding problem. If I return only one item from $someArray[0]["some_key"] , Chrome shows "USA" with a Content Length of 5, so I'm sure there are no characters other than ASCII. However, Firefox shows null with a content length of 4.
  • Doing wget gives me the correct content with all JSON. Since I trust wget more than the browser, I assume this is not a Drupal / .htaccess problem.
  • JSON is well-formed according to jsonlint.com, and since the PHP function performs the conversion, I assume it is really well-formed.
  • other things, such as permissions (everyone is allowed to connect to the page) or encoding (sending UTF-8), does not change the result.
  • This is not related to jQuery / cross-domain since I just want to call the url in the browser and want to see the JSON response.
  • On another machine with the same setup (Drupal), the result will be the same.
  • I can return JSON from other directories that are not related to Drupal setup. But since I'm just doing json_encode , I will get around every possible Drupal output, and since wget works, I am not involved with Drupal IMO.

updates according to comments

  • An application/json content type with appropriate encoding information. Changing it to text / html or something else does not change anything. Using both the header and the Drupal function to customize the headers.
  • I am sure the null answer is correct, since I tested it with Firebug.
  • ini_set('default_charset', 'UTF-8'); nothing changes, as I am already sending this information to the header.

Answer 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
Although Chrome shows the correct (well-formed) JSON output, Firefox (as well as verified in Opera) shows only null even for the simplest string, such as "USA" .

+4
source share
3 answers

The problem was that Firefox somehow did not send the correct language to the server. I do not know, through cookies or inside headers. Since I used filtering that recognizes the language for output, the output never returned.

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

+1
source

As I said in my comment, there may be a problem with the way you send the json response to the browser

this is not a coding problem. If I return only one item from $ someArray [0] ["some_key"], Chrome shows "USA" with a content length of 5, so I'm sure there are no characters other than ASCII. However, Firefox shows null with a content length of 4.

null returns content length 4

US returned content length 5? What for? USA have only 3 characters

Check white interval before or after response

0
source

Sending json file to header for using XMLHttpRequest

 $json_string = file_get_contents("path/well_formated.json"); 

using

 header("header_name: $json_string")); 

not performed for ff and

 header("header_name:". json_encode($json_string)); 

not performed for everyone

php HEREDOC solution

 $json_string = file_get_contents("path/well_formated.json"); $h= <<<H header_name : $json H; header($h); 

This will work well in Chrome, Opera, Firefox, IE

0
source

Source: https://habr.com/ru/post/1432063/


All Articles