Solving this issue of sending arabic characters using ajax

前端 未结 4 2140
臣服心动
臣服心动 2021-01-05 14:02

I\'m using an AJAX form in order to send data to another page named \'show.php\'. Here is the source of pages:

form.html



        
相关标签:
4条回答
  • 2021-01-05 14:25
    <?php
      $orignialmytext = $_REQUEST['mytext'];
      $decodedmytext = urldecode($orignialmytext);
      echo $decodedmytext;
    ?>
    
    0 讨论(0)
  • 2021-01-05 14:26

    You're using escape() and some fancy custom replacements. Don't do this.

    escape() is broken and there is very little reason to use it.

    The function you're looking for is called encodeURIComponent().

    // use an array to hold the query string parts
    var qstr = [];
    
    qstr.appendParam = function(name, value) {
      this.push( 
         encodeURIComponent(name) 
         + (value > "" ? "=" + encodeURIComponent(value) : "")
      );
      return this;
    }
    qstr.toString = function () {
      return "?" + this.join("&");
    }
    
    // use like this:
    qstr.appendParam("foo", "bar");
    qstr.appendParam("arabic", "سلام. چطوری");
    
    // now make a real query string.
    qstr.toString() // "?foo=bar&arabic=%D8%B3%D9%84%D8%A7%D9%85.%20%DA%86%D8%B7%D9%88%D8%B1%DB%8C"
    

    The above should replace your GetElemValue() function. Note how you can tweak objects by adding functions you need (like appendParam()) or overriding functions that are already there (like toString()).

    Also note that you can return the array itself from your function getquerystring(). JavaScript calls toString() automatically in situations like this:

    var url = "http://bla/" + qstr
    

    Since toString() is overridden, the right thing will happen.

    0 讨论(0)
  • 2021-01-05 14:27

    I think this may help:

    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');

    The above code should set the character set of the AJAX Request to UTF-8.

    0 讨论(0)
  • 2021-01-05 14:30

    What is happening here is your page is encoding the characters as unicode. For example, %u0633 is the first character in your string. That is normal, though I am surprised it is happening automatically.

    Now, you need to decode them when displaying to the viewer.

    It looks like this may be what you want: http://www.php.net/manual/en/function.utf8-decode.php

    string utf8_decode ( string $data )
    

    That function takes encoded input, which looks like "%u0079" or "%u0078" and turns it back into letters. When you try to display the string using PHP, wrap it in:

    utf8decode("mystring")
    

    I think show.php should look like:

    <?php
      echo utf8decode($_REQUEST['mytext']);
    ?>
    
    0 讨论(0)
提交回复
热议问题