代码之家  ›  专栏  ›  技术社区  ›  codeNinja

在javascript中编码URL而不是编码&

  •  1
  • codeNinja  · 技术社区  · 7 年前

    我正在用javascript编码以下字符串

    encodeURI = "?qry=M & L";
    

    这给了我一个输出

    qry=m%20&%20l
    

    所以来自的“&” M & L 没有被编码。我该怎么办?

    5 回复  |  直到 7 年前
        1
  •  1
  •   Code Maniac    7 年前

    Reference

    encodeURI

    A-Z a-z 0-9 ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) #
    

    let uri = "?qry=M & L"
    
    console.log(encodeURI(uri))

    encodeURIComponent

    A-Z a-z 0-9 - _ . ! ~ * ' ( )
    

    let uri = "?qry=M & L"
    
    console.log(encodeURIComponent(uri))
        2
  •  1
  •   Dhananjai Pai    7 年前

    & %26 ? =

    let uri = "?qry=M & L"
    
    console.log(encodeURIComponent(uri))
        3
  •  1
  •   Fullstack Guy    7 年前

    encodeURI() & & encodeURIComponent

    encodeURI

    A-Z a-z 0-9 ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) #
    

    A-Z a-z 0-9 - _ . ! ~ * ' ( )
    

    console.log(encodeURIComponent("?qry=M & L"));

    const URL = "https://www.example.com/resource?query=10&id=20&name=hello%"
    
    console.log(encodeURI(URL));
    console.log(encodeURIComponent(URL));

    MDN

        4
  •  0
  •   Bharat Raj Saya    7 年前

    here

    encodeURIComponent()

    this answer

    var inputWithSplChars = "M & L";
    encodeURI  = "?qry=" + encodeURIComponent(inputWithSplChars);
    

        5
  •  0
  •   Narendra Kamath    7 年前