English 中文(简体)
Ajax 请求的 Django 字符错误编码
原标题:Django character wrong encoding for Ajax request

我有一个由数据库生成的表格。在数据库中,我有一个字符串,比如西班牙语,它将成为下调菜单中的选项。

那一刻,我的html看起来像:

<option value="Española">Española</option>

我使用这些值作为表格中需要发送 AJAX 请求的动态部分。

我可以看到,当使用 IE 时, 信头就像这样:

GET /collections/find_island?island_group=Espa�ola HTTP/1.1" 500 63206

何时应:

GET /collections/find_island/?island_group=Espa%C3%B1ola HTTP/1.1" 200 164

由其它浏览器生成。

我能不能在我的模板中找到这个输出:

<option value="Espa%C3%B1ola">Española</option>

任何帮助 非常感谢。

编辑:

我的形状:

def form(forms.Form):
    ...
    island_group = forms.ModelChoiceField(
    required=False,
    label=ugettext_lazy( Island Group ), 
    initial=None,
    queryset=Localityonline.objects.values_list( islandgroup , flat=True).distinct( islandgroup ).order_by( islandgroup ), 
    empty_label=ugettext_lazy("Not Specified"), 
    widget=forms.Select(attrs={"class": searchfield , "onChange": getIslandName() })
)

笔记本:

function getIslandName(lang) {
var islandGroup = document.getElementById("id_island_group").value;

if (islandGroup ==   ) {
    // if Not Specified re-selected then set data to null and bypass updatePage()
    var data = null; 
    update_select($( select[name=island_name] ), data);
} 
else {
    var url = "../collections/find_island?island_group=" + islandGroup;
    request.open("GET", url, true);
    request.onreadystatechange = updatePage;
    request.send(null);
    }    
}
最佳回答

您可以在 javascipt 中调用 < code> encodeURI 来给出您正在寻找的编码值。 也许 Mozilla 和 铬会自动操作它, 而 IE 却没有吗?

encodeURI( Española )
// "Espa%C3%B1ola"

var url = "../collections/find_island?island_group=" + encodeURI(islandGroup);

或编码整个URL 我不知道哪个更有意义...

< a href=>"https://stackoverflow.com/ questions/332872/how-to-encode-a-url-in-javascript">Encode URL in JavaScript?

https:// developmenter.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIconcomponent\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

https:// developmenter.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURI" rel=“不跟随Noreferr'>https://developter.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURI

问题回答

暂无回答




相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...

热门标签