Solving this issue of sending arabic characters using ajax

前端 未结 4 2253
臣服心动
臣服心动 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条回答
  •  旧时难觅i
    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.

提交回复
热议问题