javascript 如何使用 GET 作为方法在 Ajax 请求上发送方括号“%5B”“%5D”作为参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19534537/
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 do I send brackets "%5B" "%5D" as params on an Ajax Request using GET as method?
提问by Pompeyo
Using Ajax and the method GET, I am trying to send an url with brackets, but I am not getting the right encoding of them:
使用 Ajax 和 GET 方法,我试图发送一个带括号的 url,但我没有得到正确的编码:
Request URL:http://myurl/search.html?_dc=1382510050331&search%5Bpostcode%5D=96231
instead of:
代替:
Request URL:http://myurl/search.html?_dc=1382510050331&search[postcode]=96231
Error:
错误:
Status Code:502 Host not found
Here is a snippet of my code:
这是我的代码片段:
Ext.Ajax.request({
url: 'http://myulr.lan/fpsearchjson.html',
method: 'GET',
params: {
"geosearch[postcode]":91111
},
success: function(response){
console.log("success");
},
failure: function(response){
console.log("failure");
}
});
Any help will be appreciated!
任何帮助将不胜感激!
回答by Filip Haglund
%5B
and %5D
are the url-encoded values of [
and ]
. This should be encoded like it is in your example.
%5B
和%5D
是的URL编码的值[
和]
。这应该像在您的示例中一样进行编码。
The problem seems to be that you are unable to reach the server. Try to reach the server in any way. Maybe open the URL in your favorite browser or telnet to it: telnet my.server.com 80
问题似乎是您无法访问服务器。尝试以任何方式访问服务器。也许在您喜欢的浏览器中打开 URL 或 telnet 到它:telnet my.server.com 80
回答by Muhammad Hussain wazir
You can use escape function to encode ,decode your url and parameter. On other side you can easily get that value in original format
for example
您可以使用转义函数对您的 url 和参数进行编码、解码。另一方面,您可以轻松地以原始格式获得该值,
例如
escape("It's me!") // result: It%27s%20me%21
回答by Naveed
You need to convert your Get request though ajax should first convert to ASCII, same problem happen to me I solve it though convert my GET request into ASCII and again decode for use :)
你需要转换你的 Get 请求,虽然 ajax 应该首先转换为 ASCII,同样的问题发生在我身上我解决了它,虽然将我的 GET 请求转换为 ASCII 并再次解码以供使用:)