简体   繁体   English

Django:JSON和Internet Explorer问题

[英]Django: Issue with JSON and Internet Explorer

have a problem with JSON not working with Internet Explorer (IE9). JSON无法与Internet Explorer(IE9)配合使用时出现问题。 I've searched the web alot, and tried the different solutions, but nothing seems to work for me. 我在网上搜索了很多,并尝试了不同的解决方案,但似乎没有任何效果对我有用。 So thought it was time to create a SO account and ask my first question. 因此以为是时候创建一个SO帐户并询问我的第一个问题了。

I'm using JSON and jQuery/AJAX to create a simple upload progress indicator The indicator works fine in Firefox and Chrome 我正在使用JSON和jQuery / AJAX创建一个简单的上传进度指示器该指示器在Firefox和Chrome中正常工作

This is my view function: 这是我的视图功能:

@never_cache
@admin_required
@render_to("editor/edit_file.html")
def edit_file(request, file_id=None):
    if file_id:
        try:
            file = Document.objects.get(pk=file_id)
            form = DocumentForm(request.POST or None, files=request.FILES or None, instance=file)
        except Document.DoesNotExist:
        form = DocumentForm(request.POST or None, files=request.FILES or None)
    else:
        dir_id = request.GET.get("dir")
        if dir_id:
            try:
                dir = Directory.objects.get(pk=dir_id)
                form = DocumentForm(request.POST or None, files=request.FILES or None, initial ={"directory":dir_id, "available_to":dir.available_to})
            except:
                form = DocumentForm(request.POST or None, files=request.FILES or None)
        else:
            form = DocumentForm(request.POST or None, files=request.FILES or None)

    result = {}
    response = locals()

    if request.method == "POST":
        if form.is_valid():
            file = form.save()
            result["status"] = "ok"
            result["href"] = reverse("portal.editor.views.files_list", args=[file.directory.pk])
            if not request.is_ajax():
                response = HttpResponseRedirect(reverse("portal.editor.views.files_list", args=[file.directory.pk]))
        else:
            result[status] = "error"
        if request.is_ajax():
            response = HttpResponse(json.dumps(result), mimetype="text/plain")
            add_never_cache_headers(response)
    return response

and here is my jQuery code: 这是我的jQuery代码:

if ($("#id_edit_form").length > 0) {
    $("#id_edit_form").ajaxForm({
        type: "POST",
        cache: false,
        dataType: "json",
        beforeSubmit:  function (formData, jqForm, options) {
            $("#id_overlay").height($(document).height()).show();
        },
        success: function (response, status, xhr, $form) {
            $("#id_overlay").hide();
            if (status === "success") {
                if (response.status === "ok") {
                    location.href = response.href;
                } else if (response.status === "error") {
                    for (var item in response.form) {
                        $("#id_" + item).parent("p").next("p.js_form-error").html(response.form[item][0]);
                    }
                } else { alert(response.result); }
            } else { alert("Error status:", status); }
        }
    });
}

I feel like I've tried everything. 我觉得我已经尝试了一切。 Also tried changing mimetype to "text/plain" and it still didn't work. 还尝试将mimetype更改为“ text / plain”,但仍然无效。 I'm using Django 1.3, Python 2.6 and jQuery 1.6.1 with nginx server. 我在nginx服务器上使用Django 1.3,Python 2.6和jQuery 1.6.1。

What about mimetype in nginx/mime.types? 那么nginx / mime.types中的mimetype呢? Should I add json in this file? 我应该在此文件中添加json吗? I really need help with this, since I've been tearing my head of due to this problem! 我真的需要这个帮助,因为由于这个问题我一直在哭!

Thanks in advance 提前致谢

the syntax error was probably that comma after last } 语法错误可能是最后一个逗号之后的逗号}

success:       function(response, status, xhr, $form){
            $("#id_overlay").hide();
                if( status == "success" ){
                    if( response.status == "ok"){
                        location.href = response.href;
                    }else
                    if(response.status == "error" ){
                        for(var item in response.form){
                            $("#id_"+item).parent("p").next("p.js_form-error").html(response.form[item][0]);
                        }
                    }else
                        alert(response.result);
                }else
                    alert("Error status:", status);
        },

THAT last comma. 最后一个逗号。 IE is very picky about such stuff. IE对此类内容非常挑剔。

I did some reading about ajaxForm that you are using. 我读了一些有关您正在使用的ajaxForm的文章。 Is this that : http://malsup.com/jquery/form/ ? 这是: http : //malsup.com/jquery/form/吗? Seems to me that you could use it much easyer or forget about it completely and use jquery ajax like this: 在我看来,您可以更轻松地使用它,也可以完全忘记它,然后像这样使用jquery ajax:

$('a.submit').live('click', function(event){
    event.preventDefault();
    var f = $(this).parents('form');
    data = f.serialize();
    if (contactIsWorking == false){
        $.ajax({
            'type':'POST',
            'url': '/contactform/',
            'data': data,
            'datatype':'json',
            'beforeSend':function(){
                contactIsWorking = true;
            },
            'success':function(data, textStatus){
                postHandler(f, data);
            },
            'error':function(jqXHR, textStatus, errorThrown){

            },
            'timeout':function(data){
            },
            'complete':function(data){
                contactIsWorking = false;
                spinner.fadeOut(500);
            }
        });
    }
});

Took this code quickly from one of my projects - it probably has too much info but i guess you can filter out whats needed 从我的一个项目中快速获取了此代码-它可能包含太多信息,但我想您可以过滤出所需内容

FYI, this bug went away when I upgraded to Django 1.5. 仅供参考,当我升级到Django 1.5时,此错误消失了。

Seems like django 1.3 had some issues with mimetypes/content-types and JSON (now I also use built-in json instead of simplejson) 似乎django 1.3的mimetypes / content-types和JSON有一些问题(现在我也使用内置json代替了simplejson)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM