jQuery ajax双重编码问题

发布于 12-12 00:40 字数 658 浏览 1 评论 0原文

我正在尝试向服务器发送 ajax 调用,该服务器读取用户的输入文本(它是 utf-8 字符集中的希伯来语)。 我使用这样的东西:

my_url = some_url + textinput
my_url = encodeURI(my_url)

我看到的问题是,在 encodeURI 函数之后,希伯来语部分看起来像这样:%D7%9E%D7%9C%D7%95%D7% 9F(很好)。然后我使用 jquery 调用:

$.ajax({
    url: search_url,
    contentType: "application/json; charset=utf-8",
    dataType: 'json',
    error:errorData,
    success:getSearchResults
});

并且我看到在服务器上它看起来像是再次编码的,每个“%”符号现在是 %25(百分号的 utf-8 表示),我的希伯来语部分如下所示:

%25D7%259E%25D7%259C%25D7%2595%25D7%259F(每个“%”=>%25)

I找不到任何方法来克服这个问题,我希望你能:-)

干杯, 罗伊

I am trying to send an ajax call to the server which reads input text from the user (it's Hebrew in utf-8 charset).
I use something like this:

my_url = some_url + textinput
my_url = encodeURI(my_url)

The problem that I see is that after the encodeURI function, the Hebrew part looks like that: %D7%9E%D7%9C%D7%95%D7%9F (which is fine). I then use the jquery call:

$.ajax({
    url: search_url,
    contentType: "application/json; charset=utf-8",
    dataType: 'json',
    error:errorData,
    success:getSearchResults
});

and I see that at the server it looks like it was encoded again such that each "%" sign is now %25 (the utf-8 representation of percent sign) and my Hebrew part looks like that:

%25D7%259E%25D7%259C%25D7%2595%25D7%259F (each "%" => %25)

I couldn't find any way to overcome this, I hope you can :-)

Cheeers,
Roy

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

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

发布评论

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

评论(1

逆流2024-12-19 00:40:33

我认为这里最好的选择是使用 .ajax() 选项中的 data 属性。我在这里模拟了一个版本: http://jsfiddle.net/nrabinowitz/nmZra/1/< /a>

$.ajax({
    url: '/search_results/',
    data: { search: text },
    contentType: "application/json; charset=utf-8",
    dataType: 'json',
    success: $.noop
});

没有一个很好的方法让 jQuery 显示所请求的实际 URL,但是如果您查看像 Firebug 这样的控制台,您可以看到错误消息(因为这是一个伪造的 URL):

获取http://fiddle.jshell.net/search_results/?search=%D7%91%D7%92%22%D7%A5+%D7%9C%D7%9E%D7%AA%D7% 9E%D7%97%D7%99%D7%9 D%3A+%D7%97%D7%96%D7%A8%D7%95+%D7%9C%D7%94%D7%99%D7%93%D7%91%D7 %A8%D7%95%D7%AA+%D7%A2%D7%9D+%D7%94%D7%90%D7%95%D7%A6%D7%A8 404(未找到)

看起来它正确编码了希伯来语文本,没有您遇到的双重编码问题。

I think the best option here is to use the data attribute in the .ajax() options. I've mocked up a version here: http://jsfiddle.net/nrabinowitz/nmZra/1/

$.ajax({
    url: '/search_results/',
    data: { search: text },
    contentType: "application/json; charset=utf-8",
    dataType: 'json',
    success: $.noop
});

There isn't a great way to get jQuery to show the actual URL being requested, but if you look in a console like Firebug, you can see the error message (because this is a bogus URL):

GET http://fiddle.jshell.net/search_results/?search=%D7%91%D7%92%22%D7%A5+%D7%9C%D7%9E%D7%AA%D7%9E%D7%97%D7%99%D7%9D%3A+%D7%97%D7%96%D7%A8%D7%95+%D7%9C%D7%94%D7%99%D7%93%D7%91%D7%A8%D7%95%D7%AA+%D7%A2%D7%9D+%D7%94%D7%90%D7%95%D7%A6%D7%A8 404 (NOT FOUND)

It looks like it's encoding the Hebrew text correctly, without the double-encoding issue you're having.

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