为什么我的“”是在我处理 Ajax 请求的 CGI 程序中,字符变成了空格?

发布于 2024-08-25 21:30:35 字数 169 浏览 3 评论 0原文

我正在通过 Web 表单收集文本,并注意到当我的 Perl CGI 收集文本时,所有“+”实例都会转换为“”。我在提交之前通过 JavaScript 转义运行文本,但转义似乎保持不变。

一定有一些非常明显的东西我丢失了......我如何发送字符串“2 + 2 = 4”而不让它以“2 2 = 4”的形式到达?

I'm collecting text through a web form and noticing that when it is collected by my Perl CGI all instances of "+" are transformed into " ". I run the text through a JavaScript escape before submission, but escape seems to leave + unaltered.

There must be something really obvious that I'm missing... how do I send the string "2 + 2 = 4" through and not have it arrive as "2 2 = 4"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浊酒尽余欢 2024-09-01 21:30:36

我不知道你所说的使用 JavaScript 转义是什么意思。浏览器将正确编码表单字段值,CGI.pm 将正确解码它们。

例如,

#!/usr/bin/perl

use strict; use warnings;
use CGI;

my $cgi = CGI->new;

if ( $cgi->param ) {
    process_form($cgi);
}
else {
    show_form($cgi);
}

sub process_form {
    my ($cgi) = @_;

    print $cgi->header('text/plain'),
          $cgi->param('entry'), "\n";
}
sub show_form {
    my ($cgi) = @_;

    print $cgi->header, <<EO_HTML;
<!DOCTYPE HTML>
<html><head><title>Test +</title></head>
<body><form>
<input name="entry"><input type="submit">
</form></body></html>
EO_HTML
}

我通过在输入字段中提交此表单并使用 2+2=4 获得的输出是:

2+2=4

I do not know what you mean by using JavaScript escape. Browsers will properly encode form field values and CGI.pm will properly decode them.

For example,

#!/usr/bin/perl

use strict; use warnings;
use CGI;

my $cgi = CGI->new;

if ( $cgi->param ) {
    process_form($cgi);
}
else {
    show_form($cgi);
}

sub process_form {
    my ($cgi) = @_;

    print $cgi->header('text/plain'),
          $cgi->param('entry'), "\n";
}
sub show_form {
    my ($cgi) = @_;

    print $cgi->header, <<EO_HTML;
<!DOCTYPE HTML>
<html><head><title>Test +</title></head>
<body><form>
<input name="entry"><input type="submit">
</form></body></html>
EO_HTML
}

The output I get from submitting this form with 2+2=4 in the entry field is:

2+2=4
终难愈 2024-09-01 21:30:35

转义和取消转义函数对于非 ASCII 字符无法正常工作,已被弃用。在 JavaScript 1.5 及更高版本中,使用encodeURI、decodeURI、encodeURIComponent 和decodeURIComponent。

https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Predefined_Functions /escape_and_unescape_Functions

escape 替换为 encodeURIComponent

The escape and unescape functions do not work properly for non-ASCII characters and have been deprecated. In JavaScript 1.5 and later, use encodeURI, decodeURI, encodeURIComponent, and decodeURIComponent.

https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Predefined_Functions/escape_and_unescape_Functions

Replace escape with encodeURIComponent

暖树树初阳… 2024-09-01 21:30:35

URL 中的“+”应编码为 %2B

http://www.example.com/myscript.ext?a=2%20%2B%202%20=%204

将给出 a = 2 + 2 = 4

在 Javascript 中,这意味着使用 encodeURIComponent 函数:这部分代码:

encodeURIComponent('2 + 2 = 4')

将给出:

"2%20%2B%202%20%3D%204"

注意 + 已编码。

虽然这个 :

escape('2 + 2 = 4')

只会给出 :

"2%20+%202%20%3D%204"

请注意 + 未编码。

You '+', in the URL, should be encoded as %2B :

http://www.example.com/myscript.ext?a=2%20%2B%202%20=%204

Will give a = 2 + 2 = 4

In Javascript, this means using the encodeURIComponent function : this portion of code :

encodeURIComponent('2 + 2 = 4')

will give :

"2%20%2B%202%20%3D%204"

Note the + is encoded.

While this one :

escape('2 + 2 = 4')

would only give :

"2%20+%202%20%3D%204"

Note the + is not encoded.

庆幸我还是我 2024-09-01 21:30:35

您可以将 + 编码为 %2B,如下所示:http://www.google.com/search?q=2+%2B+2

You can encode + as %2B, as seen in: http://www.google.com/search?q=2+%2B+2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文