将 UTF8 编码的 JSONP 注入 Win 1255 编码的网页时出现问题
我正在开发一个作为 JS 片段嵌入到网站中的第三方服务,其中需要从我的 PHP 服务器获取一些 jsonp 数据,并在托管嵌入网站上显示 json 对象中包含的文本。
我正在使用 jQuery,所以我发出以下 .getJSON 请求:
$.getJSON("http://localhost/php/server.php?a=gfs"+"&callback=?",function(Obj) {
doSomething(Obj);
});
在 PHP 端 (server.php) 我有:
<?php
header('Content-Type: text/javascript; charset=utf8');
$retval = file_get_contents('../scripts/file.json');
//change to json php
$callback = $_GET['callback'];
echo $callback . '(' . $retval . ')';
?>
这些在 FF 中完美运行,但当嵌入网站使用与 utf8 不同的内容进行编码时,在 IE 中会失败,特别是Windows 1255(希伯来语)网页,即 file.json 中包含的文本显示为乱码。将网站的编码(在浏览器中,而不是源代码中)更改为 unicode“修复”了 json 显示文本的问题,同时当然使页面的其余部分看起来像乱码......我也遇到了类似的问题与 FF 一起,在我将 header(...) 行添加到 php 脚本之前。
我应该怎么办?有谁知道为什么它在 FF 中运行良好而在 IE 中运行不佳?是否有额外的定义,例如 IE 所需的 header(...) ?
限制条件: 我无法控制嵌入网站 file.json 必须以 utf8 编码(这就是我的数据库的工作方式) 相同的代码需要能够处理 utf8 编码的页面和非 utf8 页面
I am developing a third-party service embedded in websites as a JS snippet, that amongst other things need to fetch some jsonp data from my PHP server, and display the text contained in the json object on the hosting embedding website.
I am using jQuery, so I issue the following .getJSON request:
$.getJSON("http://localhost/php/server.php?a=gfs"+"&callback=?",function(Obj) {
doSomething(Obj);
});
and on the PHP side (server.php) I have:
<?php
header('Content-Type: text/javascript; charset=utf8');
$retval = file_get_contents('../scripts/file.json');
//change to json php
$callback = $_GET['callback'];
echo $callback . '(' . $retval . ')';
?>
These work perfectly in FF, but fail in IE when the embedding website is encoded with something different than utf8, specifically a Windows 1255 (Hebrew) web page, in the sense that the text contained in file.json is displayed as gibberish. Changing the encoding of the website (in the browser, not the source) to unicode "fixes" the problem with the displayed text from the json, while of course makes the rest of the page look like gibberish... I had a similar problem with FF, before I added the header(...) line to the php script.
What should I do? Does anyone know why it works well in FF and not in IE? Is there an additional definition such as the header(...) one required for IE specifically?
Constraints:
I have no control on the embedding website
file.json has to be encoded in utf8 (that's how my db works)
The same code needs to be able to handle both utf8 encoded pages and non utf8 pages
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
呃。
看来 IE 的修复正在更改标头定义:
header('Content-Type: text/javascript; charset=utf8');
到
header('Content-Type: text/javascript; charset=utf-8');
是的,字符集名称中缺少“-”。 IE 不能理解 utf8(不带破折号),而 FF 可以理解。
喜悦。
希望这有时会对某人有所帮助,并节省一些浪费的时间。
urgh.
It seems that the fix for IE is changing the header definition from:
header('Content-Type: text/javascript; charset=utf8');
to
header('Content-Type: text/javascript; charset=utf-8');
yep, the missing "-" in the charset name. turns ot the utf8 (without the dash) is not understood by IE, while it is understood by FF.
The joy.
Hope this might prove helpful to someone, sometime, and save some wasted time.