AJAX with ASCII above 128 and below 255

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    AJAX with ASCII above 128 and below 255

    In the Database, I save some characters like £, ¥, ¼, ½, ¾.

    When I display them directly in my HTML, they are fine.

    But when I'm using AJAX to bring this string from PHP and display it, its displaying unknown characters (� � � � �). Rest of the string is being displayed fine, the problem is only with these characters.

    How can I solve this?
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hi ...

    it seems to be an character encoding problem with the response. i think that the XMLHttpRequest always uses UTF-8 encoding and what is the encoding of your page?

    kind regards

    Comment

    • hsriat
      Recognized Expert Top Contributor
      • Jan 2008
      • 1653

      #3
      Originally posted by gits
      hi ...

      it seems to be an character encoding problem with the response. i think that the XMLHttpRequest always uses UTF-8 encoding and what is the encoding of your page?

      kind regards
      Its UTF-8, as far as page is concerned.
      But I don't think that should effect characters below ASCII 255.


      And is there anything to be amended in this line?

      xmlHttp.setRequ estHeader("Cont ent-type", "applicatio n/x-www-form-urlencoded");

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        hmmm ... may be you could encode them at serverside as html-entities (like this)? ...

        Comment

        • hsriat
          Recognized Expert Top Contributor
          • Jan 2008
          • 1653

          #5
          Originally posted by gits
          hmmm ... may be you could encode them at serverside as html-entities (like this)? ...
          No, its not working... it's converting characters like © to ©

          This is actually creating problem coz I'm doing an htmlescape filter before displaying the text.

          I need to do something else... Any other solution? :(

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Originally posted by hsriat
            And is there anything to be amended in this line?

            xmlHttp.setRequ estHeader("Cont ent-type", "applicatio n/x-www-form-urlencoded");
            Try:
            [code=javascript]xmlHttp.setRequ estHeader("Cont ent-type", "applicatio n/x-www-form-urlencoded; charset=utf-8");[/code]

            Comment

            • hsriat
              Recognized Expert Top Contributor
              • Jan 2008
              • 1653

              #7
              Originally posted by acoder
              Try:
              [code=javascript]xmlHttp.setRequ estHeader("Cont ent-type", "applicatio n/x-www-form-urlencoded; charset=utf-8");[/code]
              no, thats not working.

              Actually, if I add meta tag to the HTML, for charset=utf-8, it doesn't work at all.

              But if I give no meta tag for charset, it works fine with such characters when they are displayed directly without using AJAX.
              With AJAX, they are giving problem in both cases.

              So I think, for AJAX I need to change charset=ISO-8859-1.

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Originally posted by hsriat
                So I think, for AJAX I need to change charset=ISO-8859-1.
                That should do the trick then. Did it work?

                Comment

                • hsriat
                  Recognized Expert Top Contributor
                  • Jan 2008
                  • 1653

                  #9
                  Originally posted by acoder
                  That should do the trick then. Did it work?
                  Tried both..[code=javascript]xmlHttp.setRequ estHeader("Cont ent-type", "applicatio n/x-www-form-urlencoded; charset=ISO-8859-1");
                  xmlHttp.setRequ estHeader("Cont ent-type", "applicatio n/x-www-form-urlencoded; charset:ISO-8859-1");[/code]But no one worked.

                  Comment

                  • hsriat
                    Recognized Expert Top Contributor
                    • Jan 2008
                    • 1653

                    #10
                    Quite an old thread, but I figured out the solution today when was working on this again.

                    I was using escape(), but had to use encodeURICompon ent() instead.

                    Thank you acoder for letting me know the difference between these two in some other thread.

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #11
                      Well, that's great. I don't know why I didn't think of that. Thanks for posting the solution.

                      Comment

                      Working...