Solve the problem that JS parameter contains Chinese garbled characters when passing parameters in URL

Source: Internet
Author: User

Requires two times encodeURI () encoding and two decodeURI () decoding,

When using encodeURI () encoding,

var searchtype = $ ("#type_select option:selected"). Val (); // "Base News"    var searchcontent = $ ("#search_val"). Val (); // "AAAA"    var url = encodeURI ("textsearchdetail.aspx?searchtype=" + searchtype + "&content=" + searchcontent);    var enurl =   encodeuri (URL),//used two times Encoderui to encode     = Enurl;

When using decodeURI () decoding,

$(function () {    varPostData =getrequest ();    Console.log (PostData);    Console.log (Postdata.searchtype); Console.log (postdata.content);})functiongetrequest () {var url =decodeuri (decodeURI (Location.search)); //gets the URL in the "?" The string after the Decoderui, using two times the decoding     varTherequest =NewObject (); if(Url.indexof ("?")! =-1) {        varstr = URL.SUBSTR (1); STRs= Str.split ("&");  for(vari = 0; i < strs.length; i++) {Therequest[strs[i].split ("=") [0]] = unescape (strs[i].split ("=") [1]); }        returntherequest; }}

The output is:

Object {searchtype: "base dynamic", Content: "AAAA"}

  

Solve the problem that JS parameter contains Chinese garbled characters when passing parameters in URL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.