jQuery ajax双重编码问题
我正在尝试向服务器发送 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
我认为这里最好的选择是使用
.ajax()
选项中的data
属性。我在这里模拟了一个版本: http://jsfiddle.net/nrabinowitz/nmZra/1/< /a>没有一个很好的方法让 jQuery 显示所请求的实际 URL,但是如果您查看像 Firebug 这样的控制台,您可以看到错误消息(因为这是一个伪造的 URL):
看起来它正确编码了希伯来语文本,没有您遇到的双重编码问题。
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/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):
It looks like it's encoding the Hebrew text correctly, without the double-encoding issue you're having.