JQuery AJAX is not sending UTF-8 to my server, only in IE

54,192

Solution 1

Try encoding the query parameter with encodeURIComponent()

data:"q="+encodeURIComponent( query )

as bobince very correctly noted in his comment, if you use the object notation to pass parameters to the ajax method it will handle the encoding itself..

so

data:{ q : query }

will make jQuery handle the encoding ..

Solution 2

I'we read this post hoping it would solve the problem I had came across and that had to do with utf8 conversions.

In my case it turned out that the server engine (node.js) calculating the Content-length of the data with the data considered to be raw and not utf8, thus two character extended chars in uft8 was calculated as if they where one char resulting in the server sending one character too little.

See what I did to solve it here: Not well formed Json when sending to CouchDB

Solution 3

I know this is an old post but I had this problem recently and I'd like to contribute just in case someone else has the same problem. I'm using PHP but I'm sure there's an option on every serverside language. It was just a couple of things:

  1. Make sure you're sending the right headers on your ajax response by adding header('Content-Type: text/html; charset=utf-8'); This must be your first line. If you have any errors saying that headers have been sent already or something like that is because somewhere in your code you are outputing an extra space or something before sending the header so check your code.

  2. When you build your response in your server, make sure you convert all your chars to the correspondig HTML char using echo htmlentities($your-string, null, 'utf-8); Because even after telling IE that you are sending utf-8 data, it seems like IE forgets that or it doesn't simply assume anything so adding this to your code will ensure the right output.

Thanks all for your help.

Share:
54,192
TIMEX
Author by

TIMEX

Updated on July 25, 2022

Comments

  • TIMEX
    TIMEX almost 2 years

    I am sending UTF-8, japanese text, to my server. It works in Firefox. My access.log and headers are:

    /ajax/?q=%E6%BC%A2%E5%AD%97
    Accept-Charset  ISO-8859-1,utf-8;q=0.7,*;q=0.7
    Content-Type    application/x-www-form-urlencoded; charset=UTF-8
    

    Howeer, in IE8, my access.log says:

    /ajax/?q=??
    

    For some reason, IE8 is turning my AJAX call into question marks. Why!? I added the scriptCharset and ContentType according to some tutorials, but still no luck.

    And this is my code:

    $.ajax({
        method:"get",
        url:"/ajax/",
        scriptCharset: "utf-8" ,
        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        data:"q="+query ...,
        ...
        })