views:

450

answers:

3

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 ...,
    ...
    })
+4  A: 

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 ..

Gaby
+1 This was my first thought, but I thought jQuery handled the encoding. Now it actually makes sense that jQuery wouldn't handle the encoding if data was already set to a query string. Duh!
Andy E
@Andy, I had the same thoughts at start, but then noticed the actual code.. :)
Gaby
Yep, this is a good reason to pass a mapping like `{q: query}` into `ajax()` and let jQuery worry about the encoding for you.
bobince
@bobince, +1, very true and i will add this to the answer for future reference.
Gaby
A: 

Great answer thanks a lot, encodeURIComponent solved my problem with IE

Dave
This isn't a forum, thanks should go in the comments, not the answers section.
Matt Huggins
A: 

encodeURIComponent also fixed this for me. THANKS! Spent HOURS! looking around.

Graham