javascript 从 get 方法中删除 %20 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8558771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:47:54  来源:igfitidea点击:

Removing %20 value from get method

javascriptandroidget

提问by Smitha

Removing %20 in get method?

在 get 方法中删除 %20?

var c=new Array(a);    
(eg: a={"1","2"})  window.location="my_details.html?"+  c + "_";  

and in my_details.html :

在 my_details.html 中:

var q=window.location.search;    
alert("qqqqqqqqqqqqq " + q);   
var arrayList = (q)? q.substring(1).split("_"):[];      
var list=new Array(arrayList);    
alert("dataaaaaaaaaaaa " +  list  + "llll " ); 

and in "list" its dusplaying me "1%202";

在“列表”中,它在欺骗我"1%202"

How can I remove this %20 =spacevalue ??

我怎样才能删除这个%20 =space值?

Thanks

谢谢

回答by evildead

just use this:

只需使用这个:

alert("dataaaaaaaaaaaa " +  decodeURIComponent(list)  + "llll " );

This should decode the %20to space

这应该解码%20space

look here: http://www.w3schools.com/jsref/jsref_decodeURIComponent.asp

看这里:http: //www.w3schools.com/jsref/jsref_decodeURIComponent.asp

回答by st0le

If there is a space in the parameter(s), then the %20(URL Encoding) is necessary. You cannot pass a space in a GETrequest.

如果参数中有空格,则%20(URL Encoding) 是必需的。您不能在GET请求中传递空格。

If you need to avoid this, use POST.

如果您需要避免这种情况,请使用POST.

回答by jabclab

As far as I can see the problem is being introduced at this line:

据我所知,问题是在这一行引入的:

window.location="my_details.html?"+  c + "_";

This could be written as:

这可以写成:

window.location="my_details.html?"+  c.toString() + "_";

The default.toString()of a JavaScript Arraywould be to use a delimiter of ,, i.e.

JavaScript的默认值是使用 的分隔符,即.toString()Array,

var str = ["1", "2", "3"].toString(); // 1,2,3

In you example it appears that the delimiter being used is a space. This would have been changed by something changing the default behaviour of .toString()on the Array.prototype. Try using the following:

在您的示例中,使用的分隔符似乎是一个空格。这已被一些改变的默认行为改变.toString()Array.prototype。尝试使用以下方法:

window.location="my_details.html?"+  c.join(",") + "_";

回答by satya

Better to use replace() method to replace %20to space

最好使用 replace() 方法替换%20space

list.replace("%20"," ");

list.replace("%20"," ");