-1

When I try to get html back from a ajax request I get multiple �. Why is this and how to correct it?

    $.ajax
    ({
        type: 'POST',
        url: Generic.ajaxSluice,
        dataType: "html",
        data:
        {
            param: paramArr
        },
        success:function(response)
        {
            console.log(response);
        }
    });

This code gives me this response in the console:

[["������������������������...����������������������0", "../upload/images/Untitled-2.png"], ["1", "../upload/images/j.png"], ["2", "../upload/images/peppe.png"], ["�����������������������������������������"]]

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

1

AJAX requires UTF-8. It's not even optional. From the jQuery.ajax() manual page:

The W3C XMLHttpRequest specification dictates that the charset is always UTF-8; specifying another charset will not force the browser to change the encoding

Thus your server side code needs to read and write UTF-8. Period.


One way to verify if your server generates UTF-8 is to type a known char (I often use the symbol) and see if you get the expected bytes (E2 82 AC). To get an hex dump:

If you happen to see a lot of zeroes you're somehow getting null characters. That's a non-printable control code that should only appear on binary files (never on text). That reveals a different issue.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • contentType (default: 'application/x-www-form-urlencoded; charset=UTF-8') UTF-8 is default and i have'nt specified it in my request. Is there something wrong with the php and if so, how do i change it in php? – Kristoffer Frisell Jarnevid Jul 19 '14 at 14:30
  • You can use any encoding with this technique: http://stackoverflow.com/questions/553463/jquery-ajax-character-encoding#answer-553572 – blex Jul 19 '14 at 14:30
  • @KristofferFrisellJarnevid what matters here is the encoding that your **server** is actually using. – Pointy Jul 19 '14 at 14:33
  • @KristofferFrisellJarnevid Declaring your encoding is one thing. Actually using it is a different one. PHP has `bin2hex()`. Just type a known char (e.g. `€`) and see if you get the expected bytes (`e282ac`). – Álvaro González Jul 19 '14 at 14:34
  • @blex According to the jQuery docs, that will only work in some browsers in the best case. – Álvaro González Jul 19 '14 at 14:36
  • It returns e282ac. This is what i get: 0%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00e282ac%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00 .... %00 was after i used: var test = escape(response); – Kristoffer Frisell Jarnevid Jul 19 '14 at 14:48
  • Its strange. Everything is in UTF-8 and these are still there :/ What does null bytes mean? – Kristoffer Frisell Jarnevid Jul 19 '14 at 18:49
  • Okey. Yeah. I opend my docs in another app and there i found them hiding at the bottom of all docs. – Kristoffer Frisell Jarnevid Jul 19 '14 at 18:54
  • Okey. Thank you, specially Alvaro! This was solved by cleaning my php-files from ivisible bad Null characters that any of my applications have added. – Kristoffer Frisell Jarnevid Jul 19 '14 at 18:59
  • Glad you got it sorted out and thank you for reporting back. I've added the relevant information to the answer so it's more clear and useful for future readers. – Álvaro González Jul 20 '14 at 08:08