[英]URI escape / unescape from Javascript to Perl
I have an URL that javascript reads from an user input. 我有一个javascript从用户输入中读取的URL。 Here is a part of javascript code:
这是javascript代码的一部分:
document.getElementById("Snd_Cont_AddrLnk_BG").value=encodeURI(document.getElementById("Con_AddresWeb_BG").value.toString());
Then I post the value of the string through CGI to a Perl Script (here is a part of perl code): 然后,我通过CGI将字符串的值发布到Perl脚本(这是perl代码的一部分):
#!/usr/bin/perl -w
##
##
use strict;
use CGI;
use CGI::Carp qw ( fatalsToBrowser );
use URI::Escape;
my $C_AddrLnk_BG=$query->param("Snd_Cont_AddrLnk_BG");
my $lst_upload_dir="../data";
my $lst_file_bg=$lst_upload_dir."/contacts_bg.js";
open(JSBG,">$lst_file_bg") || die "Failed to open $lst_file_bg\n";
printf JSBG "var GMapLink_bg=\"".uri_unescape($C_AddrLnk_BG)."\";\n";
close JSBG;
system("chmod 777 $lst_file_bg");
Somewhere in uri_unescape a problem occurs: The original string from input is: uri_unescape中的某个地方发生问题:输入的原始字符串为:
https://www.google.bg/maps/place/42%C2%B044'15.0%22N+23%C2%B019'04.2%22E/@42.7368454,23.317962,16z/data=!4m2!3m1!1s0x0:0x0
The string after javascript encodeURI() is: javascript encodeURI()之后的字符串是:
https://www.google.bg/maps/place/42%25C2%25B044'15.0%2522N+23%25C2%25B019'04.2%2522E/@42.7368454,23.317962,16z/data=!4m2!3m1!1s0x0:0x0
And the script after perl uri_unescape() that is printed in file is: 在文件中打印的perl uri_unescape()之后的脚本是:
https://www.google.bg/maps/place/42%C2%B044'15.0%22N+23%C2%B019'04.2 0.000000E+00/@42.7368454,23.317962,16z/data=!4m2!3m1!1s0x0:0x0
I can not ascertain whether the problem is in unescaping or printing, but the part 我无法确定问题出在转义还是打印中,但是部分
%2522E
%2522E
is interpreted as 被解释为
0.000000E+00
(with 10 leading spaces). (具有10个前导空格)。
Can anyone help me with an idea of what I was doing wrong? 任何人都可以帮助我了解我做错了什么吗?
There are numerous problems with your code. 您的代码有很多问题。
document.getElementById("Snd_Cont_AddrLnk_BG").value = encodeURI(document.getElementById("Con_AddresWeb_BG").value.toString());
I can't figure out when you think encodeURI
here. 我不知道您何时想到这里的
encodeURI
。 All you should have is the following: 您应该拥有的一切如下:
document.getElementById("Snd_Cont_AddrLnk_BG").value =
document.getElementById("Con_AddresWeb_BG").value;
printf JSBG "var GMapLink_bg=\\"".uri_unescape($C_AddrLnk_BG)."\\";\\n";
Now the erroneous encodeURI
is removed, uri_unescape
needs to be removed too. 现在,错误的
encodeURI
已删除, uri_unescape
需要删除。
Furthermore, adding quotes around text doesn't always make it a valid JavaScript literal. 此外,在文本周围添加引号并不总是使它成为有效的JavaScript文字。 The easiest way to do that is as follows:
最简单的方法如下:
use JSON qw( );
my $json = JSON->new()->allow_nonref();
$json->encode($C_AddrLnk_BG)
That snippet also misuses printf
. 该代码段也滥用了
printf
。 printf
takes a format parameter, so you want printf
需要一个format参数,所以你想要
printf FH "%s", ...
or simply 或简单地
print FH ...
So what you end up with is: 因此,您最终得到的是:
use JSON qw( );
my $json = JSON->new()->allow_nonref();
$json->encode($C_AddrLnk_BG)
print JSBG "var GMapLink_bg=" . $json->encode($C_AddrLnk_BG) ."\n";
You are using printf
instead of print
to output the result of uri_unescape
. 您使用的是
printf
而不是print
来输出uri_unescape
的结果。 It is interpreting %22E
as an engineering-format floating point field with a width of 22. Presumable you have nothing else in the printf
parameter list, so undef
is being evaluated as zero, resulting in 0.000000E+00
. 它会将
%22E
解释为宽度为22的工程格式浮点字段。假定您在printf
参数列表中没有其他内容,因此undef
被评估为零,结果为0.000000E+00
。
If you had use warnings
in place as you should, you would see messages like Missing argument in printf
如果适当地
use warnings
,则会Missing argument in printf
看到诸如Missing argument in printf
消息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.