Solved

Ajax and Arabic characters

Posted on 2007-11-20
12
2,802 Views
Last Modified: 2011-09-20
Hi,
I've been learing Ajax recently and everything was going smoothly Until I faced a problem with language encoding. When my Ajax script outputs English characters I see no problem but when I try to output any document which contains Arabic characters they show like this ???????? or sometimes squares.
My code is attached with this question. Plz have a look.

Best regards,
Hakeem
<html>

<header>

<title> Ajax training </title>

	

<script language = "javascript">

	var XMLHttpRequestObject = false;

			

	if(window.XMLHttpRequest){

		XMLHttpRequestObject = new XMLHttpRequest();

	}else if (window.ActiveXObject){

		XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");

	}

		

	function tellme(dfile, did)

	{

	if(XMLHttpRequestObject) {

		var obj = document.getElementById(did);

		

		XMLHttpRequestObject.open("GET", dfile);

		

		XMLHttpRequestObject.onreadystatechange = function()

		{

		if(XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200)

		{

		obj.innerHTML = XMLHttpRequestObject.responseText;

		}

		}

		XMLHttpRequestObject.send(null);

		}

		}

</script>		

</header>
 

<body>
 

<form>

<input type="button" value="Click me !" onclick = "tellme('text.txt', 'shopies')">

</form>

<div id='shopies'>

</div>
 

</body>

</html>

Open in new window

0
Comment
Question by:Shopies
  • 6
  • 4
  • 2
12 Comments
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20321574
The fix isn't on the Javascript side or in the xmlhttp object.  It usually has no way to correct this.

How is it you are outputting the Arabic text?  I assume you are using some server language as part of it.  Which language and what does that code basically look like?

The key to solving this usually is to have the server script respond with the correct header set.  Most server languages have a way to do this and you put the line at the top of the script.  If you let me know which language you use then I can probably provide specifics on how to do it.  Take a look at the encoding used on the main page and that can give you a good idea of what to use in the script.

This will usually fix the problem.  As an example using PHP you would use a line like ...

header("Content-type: text/html; charset=iso-8859-1");

That is just one example in a specific server language but it shows the general idea.  Let me know if you have a question or need more info.  I will need answers and details from you if I will be able to be more specific in how to do it.

bol
0
 

Author Comment

by:Shopies
ID: 20321844
Hi bol,
I'm using just one file in this case whih is "index.html" And all the content of this file has been placed above. I'm using PHP but not with this example. This example is just using javascript+ajax+plain HTML. I placed a Content-Type header which correspond to my Arabic language - you can see it in the attachment - but that never solved the problem yet. Have a look to the code plz:

Regards,
Hakeem
<meta http-equiv="Content-Language" content="ar-sa">

<meta http-equiv="Content-Type" content="text/html; charset=windows-1256">

Open in new window

0
 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 500 total points
ID: 20322105
Thanks for pointing me to the file name in the first snippet and showing me the tags.

Is this really a practical example of AJAX?  I would hate for you to spend time working on something that won't really be used. :)  If you will always be using PHP or some server script when you really use AJAX then it is best to just work on that or save the concern for encoding until then.  The simple text file can be nice to test other things but can present new issues (for encoding) that would not occur with a server script.

What happens if you try to view just the text file in the browser (e.g. use http://yourdomain.com/folder/text.txt as the URL in the browser)?  How do the characters appear?  Also, do you see the html tags you posted in the last comment when you do this or use the text file in the AJAX script?

Depending on the server setup the html tags are probably being ignored because of the file extension.  If you want to test this rename the file to text.html and see what the result is.  If you open the text.txt file in an editor what is its encoding.

If it is important to get the txt file working then you might need to send it to me.  You can do this using ee-stuff.com.  That site is just for EE members and will let you upload files for a question.  You will probably need to zip the file and upload the zip but it would provide a way to work on this.  If the file is real big or has sensitive content then posting a sample that also has the same problem would be fine.

Let me know how this helps or if you have any questions.

bol
0
 

Author Comment

by:Shopies
ID: 20322277
Hi bol,
I really don't know how to thank you for this neated answer. As I'm reading your response I figured out where the problem is. The problem is as you saied that I'm trying to output a .txt file and also that file doesn't include any character definition. I renamed that text.txt file to text.html and placed the above header in it and the Arabic characters just came out corretly. Many thanks buddy.

I just started learning AJAX two days ago and I can now deal with files, PHP and so on. I placed this code because it has the problem and I understand that if I could solve the problem in it, I could deal with other scripts in the same way.

I really need your advice on where and how to begin learning AJAX. I'm reading in the book called AJAX Bible and it's fine so far. I'll start learning XML along with it. Please let me know if you have any suggestions for me, in fact I'll be more than happy.

Best Regards,
Hakeem
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20322552
Your welcome!  Thanks for the nice comment. :)  I'm glad I could help.

This site has some good AJAX info and experts to help with those issues.  One of the best explanations and simplest examples I have found on the net is at http://www.skeymedia.com/programming/classic-asp-and-ajax-tutorial/.  There is also a good explanation on what AJAX is at http://en.wikipedia.org/wiki/Ajax_%28programming%29.  The reference site W3Schools has a good section on AJAX at http://www.w3schools.com/ajax/default.asp.

The "best" example above uses ASP as the server language but that is not a big deal.  The main part of AJAX is the Javascript and clientside part and that is what I like on that site.

I haven't looked recently for books but when I did about a year ago I wasn't really impressed with anything I saw then.  That has most likely changed now and you could look at the publisher Wrox (www.wrox.com) to see if they have a book on this.  I generally like their books for tutorials and learning.

I haven't had a chance to really use XML but it will be handy with AJAX.  It isn't required though.  Other options for the server response are JSON or formatted html (or even just text).  I have used these others more than XML for my projects.

I hope these suggestions help.  You can always post a new question if you need some more help.  If you want to get my attention to a question then post the URL in an old one (like this), contact me using the EE email in my profile, or post a message in CS for a moderator to contact me.  I will paste a few of my other "favorite" AJAX URLs below.

http://www.quirksmode.org/blog/archives/2005/12/the_ajax_respon.html
http://www.quirksmode.org/blog/archives/2006/01/the_ajax_respon_1.html

There are many others too but hopefully this will get you going. :)

Enjoy AJAX!

bol
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20322558
By the way, thanks for the grade, the points and the fun question.

bol
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:Shopies
ID: 20334332
Hi bol,
Believe me if I could give you more than 500 points, I would ;)
You're so nice and helpful and I'm so lucky that someone like you passed my question by. Everything went fine because of your help and you deserve many thanks.

I also had a visit to the links you pasted. I read more and I learned more as well. I still came across another problem and I really didn't know how to solve it so I thought that bol might have the solution to that. The problem is as same as the previouse one but this time with recordes brought from the DB. I'm using PHP, MySql to handle my script.

In brief, when I fetch a record from the DB which contains Arabic characters it give me something like ????? or sometimes it show nothing. When I try to bring another English characters, Everything goes fine. I'm not using HTML at all and here is a piece of my PHP code:

<?php
      $con = mysql_pconnect("localhost","root","123") or die (mysql_error());
      mysql_select_db("shopies");
      $query = mysql_query("SELECT * FROM pics") or die (mysql_error());
      $row = mysql_fetch_array($query);
      echo $row['title'];
?>

And the Ajax code as same as above.
Could you please help me with this plz?!!

Best Regards,
Hakeem
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20335698
Hakeem,

Thanks again for the compliments. :)  I'll be happy to help with the new problem (if I can).  How about opening a new question for it. :D  Post the URL for that question here in a comment to draw my attention to it.

bol

p.s.  Did you try the PHP script I provided in http:#20321574 ?  That may be all you need to solve this with maybe a change to the encoding.  If it doesn't fix it then I'll look forward to the new question.
0
 

Author Comment

by:Shopies
ID: 20380343
Sorry bol for the delay I was busy a little bit. In fact your first solution regarding the header solved all the problemes including the HTML header problem. I used that piece of code at the very top of the all my .php files and they're working just fine now.

Thank you again bol,
Hakeem
0
 

Expert Comment

by:orid
ID: 22352156
Hi

I am struggling with the same problem:

I am trying to retrieve data from a site that contains Arabic.

But the Arabic letters appears as ??? in the responseText because the server does not set the charset
to windows-1256.

Unfortunately - I have no way to control the server side.

I have tried two alternatives.

1. Ajax.
But (if I got it right :-)  the server is using
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1256">
To set the charset to Arabic and my responseText  is not  parsed by the browser so
it does not know it has to convert the Arabic to Unicode.

2. I tried a direct post.
I used a POST with a target frame.
But when I tried to access the data in this target frame -
I discovered that I am violating the browser security.

I will appreciate any help in solving one of the above two or any other method
that is doable via JavaScript.

Thanks
Ori


 
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 22352833
Ori,
Use the "ask a related question" link/button, which you should see just at the top of the Post a Comment area.  You need to open your own question to get help on this.  Using that method will have EE notify all the participants of this question.  That should help you get immediate attention to your question and will make sure I see it.  Let me know if you have a question about it.  Make sure to include details in your Q.
bol
0
 

Expert Comment

by:orid
ID: 22352968

Thanks for the advice.
I had just posted this as a question.
I hope there is a solution.
Thanks
Ori
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now