相关疑难解决方法(0)

你什么时候应该使用escape而不是encodeURI/encodeURIComponent?

编码要发送到Web服务器的查询字符串时 - 何时使用escape()以及何时使用encodeURI()encodeURIComponent():

使用转义:

escape("% +&=");
Run Code Online (Sandbox Code Playgroud)

要么

使用encodeURI()/ encodeURIComponent()

encodeURI("http://www.google.com?var1=value1&var2=value2");

encodeURIComponent("var1=value1&var2=value2");
Run Code Online (Sandbox Code Playgroud)

javascript encoding query-string

1371
推荐指数
11
解决办法
45万
查看次数

application/x-www-form-urlencoded或multipart/form-data?

在HTTP中有两种POST数据的方式:application/x-www-form-urlencodedmultipart/form-data.据我所知,大多数浏览器只能在使用时上传文件multipart/form-data.在API上下文中使用其中一种编码类型时是否有任何其他指导(不涉及浏览器)?这可能基于:

  • 数据大小
  • 存在非ASCII字符
  • 存在于(未编码的)二进制数据上
  • 需要传输额外的数据(如文件名)

到目前为止,我基本上没有在网上找到有关使用不同内容类型的正式指导.

post http http-headers

1268
推荐指数
4
解决办法
116万
查看次数

我应该对POST数据进行URL编码吗?

我正在将数据发布到外部API(使用PHP,如果它是相关的).

我应该对我传递的POST变量进行URL编码吗?

或者我只需要对GET数据进行URL编码?

谢谢!

更新:这是我的PHP,如果它是相关的:

$fields = array(
    'mediaupload'=>$file_field,
    'username'=>urlencode($_POST["username"]),
    'password'=>urlencode($_POST["password"]),
    'latitude'=>urlencode($_POST["latitude"]),
    'longitude'=>urlencode($_POST["longitude"]),
    'datetime'=>urlencode($_POST["datetime"]),
    'category'=>urlencode($_POST["category"]),
    'metacategory'=>urlencode($_POST["metacategory"]),
    'caption'=>($_POST["description"])
);
$fields_string = http_build_query($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
Run Code Online (Sandbox Code Playgroud)

post http urlencode http-post url-encoding

119
推荐指数
2
解决办法
16万
查看次数