views:

31

answers:

1

Hi all,

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

A: 

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.

BearCode