Angular url plus sign converting to space

后端 未结 7 1581
梦如初夏
梦如初夏 2020-11-29 06:02

I have angular application where i want to pass plus sign + in query string like:

http://localhost:3000/page?name=xyz+manwal
7条回答
  •  我在风中等你
    2020-11-29 06:56

    This ia a common problem. The + character is used by the URL to separate two words. In order to use the + character in the parameter values, you need to encode your parameter values before adding them as part of the URL. Javascript / TypeScript provide a encodeURI() function for that specific purpose.

    URL encoding converts characters into a format that can be transmitted over the Internet. [w3Schools Reference]

    Here is how you can fix this problem:

    let encodedName = encodeURI('xyz+manwal');
    let encodedURI = 'http://localhost:3000/page?name='+encodedName;
    
    //.. OR using string interpolation
    let encodedURI = `http://localhost:3000/page?name=${ encodedName }`;
    

    In the same way, you can decode the parameters using decodeURI() method.

    let decodedValue = decodeURI(encodedValue);
    

提交回复
热议问题