Jump to content

Archived

This topic is now archived and is closed to further replies.

DarkReaper

Javascript + AJAX + PHP

Recommended Posts

Hello i am having a problem with ajax and php encodings ... i have a string encoded in cp1251 in a php variable, i save it to a file (using ajax and php), then with ajax i read it from the file and display it ... but only screwed up characters appears ...

The strangest thing is that if i type a message (input box, again cp1251 encoding) and then handle it to be saved to a file and display it, everything is okay ...

Do you have any ideas why is this happening?

Share this post


Link to post
Share on other sites
Hi,
That's one of the drawbacks of Ajax. Whenever you send any data with xmlhttprequest object, it's automatically converted into UTF-8 and when received it's UTF-8 encoded. I had a similar problem. I feel there is no solution to it than again converting it to your desired encoding via PHP. On MSDN I had read that Microsoft is thinking of resolving this issue in future release of IE.
Let's see when it happens

Regards
Rahul

Share this post


Link to post
Share on other sites
How can i convert it again to my desired encoding?

Example i send cp1251 encoding to ajax then when it converts it to utf8, i should convert it again to cp1251??? Am i right?

Share this post


Link to post
Share on other sites
The best way is to pass your total encoding to utf-8 which supports most of the characters.

But instead of working with encodings try replacing problematic characters with html entities. This had solved my character encoding problem from xmlhttprequest thing. In longer textes it slows but 1 or 2 seconds is acceptable if everything is seen ok.

Share this post


Link to post
Share on other sites
I think your XMLHTTPRequest object send UTF-8 to your php script. In your php script then, you can convert it to your original encoding like this:

[code=php:0]iconv("UTF-8","YOUR_ENCODING",$_GET['var'])[/code]

and do whatever you want to do with it. When you need to return info from your php script, use XML if you need to return non-ASCII chars, like this:

[code]
header('Content-Type: application/xml');
$out = '<?xml version="1.0" encoding="ORIGINAL_ENCODING?>' . "\r\n";
$out .= '<report xml:lang="LANG_HERE"><message>'.$message.'</message></report>';
echo $out;
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.