Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

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

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 ...,
    ...
    })
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...