Javascript 如何在javascript上准备编码的POST数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14830433/
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
How to prepare encoded POST data on javascript?
提问by Dmitry
I need to send data by POST method.
我需要通过 POST 方法发送数据。
For example, I have the string "bla&bla&bla". I tried using encodeURIand got "bla&bla&bla" as the result. I need to replace "&" with something correct for this example.
例如,我有字符串“bla&bla&bla”。我尝试使用encodeURI并得到“bla&bla&bla”作为结果。对于这个例子,我需要用正确的东西替换“&”。
What kind of method should I call to prepare correct POST data?
我应该调用什么样的方法来准备正确的 POST 数据?
UPDATED:
更新:
I need to convert only charachters which may broke POST request. Only them.
我只需要转换可能会破坏 POST 请求的字符。只有他们。
回答by álvaro González
>>> encodeURI("bla&bla&bla")
"bla&bla&bla"
>>> encodeURIComponent("bla&bla&bla")
"bla%26bla%26bla"
回答by Bhushan Firake
You can also use escape()function.The escape()function encodes a string.
This function makes a string portable, so it can be transmitted across any network to any computer that supports ASCII characters.This function encodes special characters, with the exception of: * @ - _ + . /
您也可以使用escape()函数。该escape()函数对字符串进行编码。此函数使字符串具有可移植性,因此它可以通过任何网络传输到任何支持 ASCII 字符的计算机。此函数对特殊字符进行编码,但以下情况除外:* @ - _ + . /
var queryStr = "bla&bla&bla";
alert(queryStr); //bla&bla&bla
alert(escape(queryStr)); //bla%26bla%26bla
Use unescape()to decode a string.
使用unescape()解码的字符串。
var newQueryStr=escape(queryStr);
alert(unescape(newQueryStr)); //bla&bla&bla
Note:
笔记:
escape() will not encode: @*/+
encodeURI() will not encode: ~!@#$&*()=:/,;?+'
encodeURIComponent() will not encode: ~!*()'
After some search on internet, I got the following:
在互联网上进行了一些搜索后,我得到了以下信息:
escape()
逃脱()
Don't use it.
不要使用它。
encodeURI()
编码URI()
Use encodeURI when you want a working URL. Make this call:
当您需要工作 URL 时,请使用 encodeURI。打这个电话:
encodeURI("http://www.google.com/a file with spaces.html")
to get:
要得到:
http://www.google.com/a%20file%20with%20spaces.html
Don't call encodeURIComponent since it would destroy the URL and return
不要调用 encodeURIComponent 因为它会破坏 URL 并返回
http%3A%2F%2Fwww.google.com%2Fa%20file%20with%20spaces.html
encodeURIComponent()
编码URI组件()
Use encodeURIComponent when you want to encode a URL parameter.
当您想对 URL 参数进行编码时,请使用 encodeURIComponent。
param1 = encodeURIComponent("http://xyz.com/?a=12&b=55")
param1 = encodeURIComponent(" http://xyz.com/?a=12&b=55")
Then you may create the URL you need:
url = "http://domain.com/?param1=" + param1 + "¶m2=99";
And you will get this complete URL:
你会得到这个完整的 URL:
http://www.domain.com/?param1=http%3A%2F%2Fxyz.com%2F%Ffa%3D12%26b%3D55?m2=99
Note that encodeURIComponentdoes not escape the ' character. A common bug is to use it to create html attributes such as href='MyUrl',which could suffer an injection bug. If you are constructing html from strings, either use " instead of ' for attribute quotes, or add an extra layer of encoding (' can be encoded as %27).
请注意,encodeURIComponent不会转义 ' 字符。一个常见的错误是使用它来创建 html 属性,例如href='MyUrl',可能会遭受注入错误。如果您从字符串构造 html,请使用 " 而不是 ' 来表示属性引号,或者添加额外的编码层(' 可以编码为 %27)。
REF:When are you supposed to use escape instead of encodeURI / encodeURIComponent?
REF:什么时候应该使用escape 而不是encodeURI / encodeURIComponent?
Also, as you are using JQuery, take a look at thisbuilt-in function.
此外,当您使用 JQuery 时,请查看此内置函数。
回答by Bhushan Firake
Use encodeURIComponent() as encodeURI() will not encode: ~!@#$&*()=:/,;?+'
使用 encodeURIComponent() 作为 encodeURI() 不会编码: ~!@#$&*()=:/,;?+'
This has been explained quite well at the following link:
这在以下链接中得到了很好的解释:
回答by jimmont
More recent DOM APIs for URLSearchParams(and via URL, possibly others too) handle encoding in some cases. For example, create or use an existing URL object (like from an anchor tag) I map entries of an object as key value pairs for URL encoded params (to use for GET/POST/etc in application/x-www-form-urlencoded mimetype). Note how the emoji, ampersand and double quotes are encoded without any special handling (copied from the Chromedevtools console):
在某些情况下,用于URLSearchParams 的更新的 DOM API (以及通过URL,可能还有其他)处理编码。例如,创建或使用现有的 URL 对象(例如来自锚标记)我将对象的条目映射为 URL 编码参数的键值对(用于 application/x-www-form-urlencoded 中的 GET/POST/etc模仿类型)。请注意表情符号、与号和双引号是如何在没有任何特殊处理的情况下编码的(从Chromedevtools 控制台复制):
var url = new URL(location.pathname, location.origin);
Object.entries({a:1,b:"",c:'"stuff&things"'}).forEach(url.searchParams.set, url.searchParams);
url.search;
"?a=1&b=%F0%9F%8D%BB&c=%22stuff%26things%22"
fetch(url.pathname, {
method: 'POST',
headers: new Headers({
"Content-type": "application/x-www-form-urlencoded"
}),
// same format as GET url search params a&b&c
body: url.searchParams
}).then((res)=>{ console.log(res); return res }).catch((res)=>{ console.warn(res); return res; });
回答by mirek
I want POST the javascript-created hidden form.
我想发布 javascript 创建的隐藏表单。
So the question is if encodeURIComponent()should be used on each POST variable.
所以问题是是否应该在每个 POST 变量上使用encodeURIComponent()。
I haven't found the answer for Dmitry's (and my) question in this thread. But I have found the answer in this thread.
我还没有在此线程中找到 Dmitry(和我的)问题的答案。但我在这个线程中找到了答案。
In case of form/POST where you have upload field(s) you must use <form enctype="multipart/form-data">, if no upload field is used, you should choose yourself as described here.
Submitting the form should do the job completly, so there is no need to use encodeURIComponent()explicitly.
<form enctype="multipart/form-data">如果您必须使用上传字段的表单/POST ,如果没有使用上传字段,您应该按照此处所述选择自己。提交表单应该可以完全完成工作,因此无需encodeURIComponent()显式使用。
If you create a Http POST without using a form or without some library which creates a Http POST from your data, then you need choose an enctype= and join data yourselves.
如果您在不使用表单的情况下创建 Http POST 或没有从您的数据创建 Http POST 的某些库,那么您需要选择一个 enctype= 并自己加入数据。
This will be easy for application/x-www-form-urlencoded, where you will use encodeURIComponent()on each value and join them exactly as for GET request.
这对于 来说很容易application/x-www-form-urlencoded,您将encodeURIComponent()在每个值上使用并完全按照 GET 请求加入它们。
If you decide use multipart/form-datathen ....? You should google more how to encode and join themin such case.
如果你决定使用multipart/form-data那么......?在这种情况下,您应该谷歌更多如何编码和加入它们。

