...

View Full Version : Ajax and Special Characters



angst
02-14-2007, 01:56 PM
Hello,
I'm having trouble with ajax return invalid characters.

it keeps returning: � � or ? ?

and it should be: © ®

any ideas as to how i can fix this?

sorry if i'm not in the right forum.

cheers,
-Ken

BonRouge
02-14-2007, 02:05 PM
I found that using utf8_encode() (http://php.net/utf8_encode) worked for me.

angst
02-14-2007, 02:07 PM
ah, unfortunately I'm using ASP right now.

shyam
02-14-2007, 02:10 PM
usually trimming helps


function trim(str) {
return str.replace(/(^\s*|\s*$)/, '');
}

angst
02-14-2007, 02:25 PM
how will trimming help with this issue?
I'm starting to think about something more along the lines of replacing all special characters with html code while they're being pulled.

but i'm open to ideas.


thanks again.
-Ken

martin_narg
02-14-2007, 03:24 PM
I wrote this string extension for JavaScript ajax calls to resolve the problem you have come across.


/* Purpose: Converts all non-latin based characters to their ASCII entity value
* Usage: [String].encode()
* Arguments: none
* Returns: String
*/
String.prototype.encode = function() {
return this.replace(/([^\x01-\x7E])/g, function(s) { return "&#"+s.charCodeAt(0)+";" });
}

var theString = "Hello!$#^*(&()))(*}{World";
var theStringForAjax = theString.encode();


For ASP just use HTMLEncode()

Dim theString, theStringForAjax
theString = "Hello!$#^*(&()))(*}{World"
theStringForAjax = HTMLEncode(theString)


Don't forget that you need to encode the string before you request it via AJAX.

Hope this helps

m_n

angst
02-14-2007, 04:41 PM
ok,
that partly works for me,

I'm putting data directly into an input field. and I get this:

The CORE™

now if i put it right to html, i get the correct result: The CORE™
this my problem is half solved.

any ideas?

thanks again!
-Ken

FJbrian
02-14-2007, 05:09 PM
http://www.ramsch.org/martin/uni/fmi-hp/iso8859-1.html
type one it appears as the other

angst
02-14-2007, 05:38 PM
Yes, i understand that it returns html character codes,
but i need it to return the valid "viewable" characters.

angst
02-15-2007, 03:21 PM
martin_narg,

the server.HTMLencode works great,
however your javascript:


String.prototype.encode = function() {
return this.replace(/([^x01-x7E])/g, function(s) { return "&#"+s.charCodeAt(0)+";" });
}

var theString = "Hello!$#^*(&()))(*}{World";
var theStringForAjax = theString.encode();



doesn't seem to be working at all. I trying just document.write(theStringForAjax), and it didn't encode at all.
am i missing something?

thanks again for all your help!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum