如何使用JavaScript安全地对URL进行编码,以便将其放入GET字符串?
var myUrl = "http://example.com/index.html?param=1&anotherParam=2";
var myOtherUrl = "http://example.com/index.html?url=" + myUrl;
Run Code Online (Sandbox Code Playgroud)
我假设你需要myUrl
在第二行编码变量?
我正在尝试编写一个用于测试的bash脚本,它接受一个参数并通过curl将其发送到网站.我需要对值进行url编码,以确保正确处理特殊字符.做这个的最好方式是什么?
这是我到目前为止的基本脚本:
#!/bin/bash
host=${1:?'bad host'}
value=$2
shift
shift
curl -v -d "param=${value}" http://${host}/somepath $@
Run Code Online (Sandbox Code Playgroud) 如何将下面的字符串转换为数组?
pg_id=2&parent_id=2&document&video
Run Code Online (Sandbox Code Playgroud)
这是我要找的阵列,
array(
'pg_id' => 2,
'parent_id' => 2,
'document' => ,
'video' =>
)
Run Code Online (Sandbox Code Playgroud) 在URL中,我应该使用%20
或编码空格+
吗?例如,在以下示例中,哪一个是正确的?
www.mydomain.com?type=xbox%20360
www.mydomain.com?type=xbox+360
Run Code Online (Sandbox Code Playgroud)
我们公司倾向于前者,但使用URLEncoder.encode(String, String)
带有"xbox 360"
(和"UTF-8"
)的Java方法返回后者.
那么,有什么区别?
我正在尝试使用新的Fetch API:https://developer.mozilla.org/en/docs/Web/API/Fetch_API
我正在做这样的GET请求:
var request = new Request({
url: 'http://myapi.com/orders',
method: 'GET'
});
fetch(request);
Run Code Online (Sandbox Code Playgroud)
但是,我不确定如何将查询字符串添加到GET请求.理想情况下,我希望能够向以下URL发出GET请求:
'http://myapi.com/orders?order_id=1'
Run Code Online (Sandbox Code Playgroud)
在jQuery中,我可以通过传递做到这一点{order_id: 1}
作为data
参数$.ajax()
.使用新的Fetch API有没有相同的方法呢?
如何将字符串中的空格转换为%20
?
这是我的尝试:
$str = "What happens here?";
echo urlencode($str);
Run Code Online (Sandbox Code Playgroud)
输出是"What+happens+here%3F"
,因此空格不表示为%20
.
我究竟做错了什么?
鉴于字符串:
"Hello there world"
Run Code Online (Sandbox Code Playgroud)
如何创建URL编码的字符串,如下所示:
"Hello%20there%20world"
Run Code Online (Sandbox Code Playgroud)
如果字符串也有其他符号,我也想知道该怎么做,比如:
"hello there: world, how are you"
Run Code Online (Sandbox Code Playgroud)
最简单的方法是什么?我打算解析,然后为此构建一些代码.