Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 ...,     ...     }) 
like image 277
TIMEX Avatar asked Mar 18 '10 21:03

TIMEX


People also ask

Can ajax send data to server?

Ajax (Asynchronous JavaScript And XML) allows web pages to be updated asynchronously by exchanging data to and from the server. This means you can update parts of a web page without reloading the complete web page.

Why is ajax success not working?

ajax post method. The reason was my response was not in the JSON format so there was no need for the dataType: 'json' line in the submit method. In my case, the returned response was in text format that's why it was not going to success event. Solution: Remove dataType: 'json' line.

Is jQuery compatible with ajax?

jQuery provides several methods for AJAX functionality. With the jQuery AJAX methods, you can request text, HTML, XML, or JSON from a remote server using both HTTP Get and HTTP Post - And you can load the external data directly into the selected HTML elements of your web page!


2 Answers

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

like image 125
Gabriele Petrioli Avatar answered Oct 25 '22 15:10

Gabriele Petrioli


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

like image 38
javabeangrinder Avatar answered Oct 25 '22 16:10

javabeangrinder