dsnnvpobaljihv3490 2012-09-03 11:40
浏览 56
已采纳

Firefox / Opera中的JSON输出为null,在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".

  • 写回答

3条回答 默认 最新

  • doureng1083 2012-09-03 13:01
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集