Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts

    Ajax and Special Characters

    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

  • #2
    Regular Coder BonRouge's Avatar
    Join Date
    Mar 2006
    Location
    Sendai, Japan
    Posts
    328
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I found that using utf8_encode() worked for me.

  • #3
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    ah, unfortunately I'm using ASP right now.

  • #4
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    usually trimming helps

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

  • #5
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    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

  • #6
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    I wrote this string extension for JavaScript ajax calls to resolve the problem you have come across.

    PHP Code:
    /* 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()
    PHP Code:
    Dim theStringtheStringForAjax
    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
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper

  • #7
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    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

  • #8
    Regular Coder
    Join Date
    Jun 2002
    Location
    Adirondacks
    Posts
    516
    Thanks
    4
    Thanked 4 Times in 4 Posts

  • #9
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    Yes, i understand that it returns html character codes,
    but i need it to return the valid "viewable" characters.

  • #10
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    martin_narg,

    the server.HTMLencode works great,
    however your javascript:
    PHP Code:
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •