$。阿贾克斯()Django的失败 [英] $.ajax() with django fails

查看:265
本文介绍了$。阿贾克斯()Django的失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用jQuery Ajax来获得Django的静态文件(JS脚本)(开发服务器),但事情是preventing的 $。阿贾克斯承诺来解决。

下面是我的ajax调用:

  $。当(
        $阿贾克斯({数据类型:脚本,缓存:真,网址:/static/ajax_upload/js/jquery.iframe-transport.js}),
        $阿贾克斯({数据类型:脚本,缓存:真,网址:/static/ajax_upload/js/ajax-upload-widget.js})
    )。然后(函数(){
        的console.log(负载下进行。);
        AjaxUploadWidget.autoDiscover();
        $(#平模)的基础(兜底,开)。
    },函数(){警报(发生错误,请尝试重新加载页面。); });
 

没有那么函数被调用,成功也不失败。

奇怪的是,自谷歌文件相同的AJAX调用工作:

  $。当(
        $阿贾克斯({数据类型:脚本,缓存:真,网址:https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js}),
        $阿贾克斯({数据类型:脚本,缓存:真,网址:https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js})
    )。然后(函数(){
        的console.log(负载下进行。);
        AjaxUploadWidget.autoDiscover();
        $(#平模)的基础(兜底,开)。
    },函数(){警报(发生错误,请尝试重新加载页面。); });
 

我认为有些事情可能是错了我的反应头,但我得到一个200状态,我不是HTTP中的专家。

这是Django的响应:

  HTTP / 1.0 200 OK
日期:星期六,2015年22时57分35秒格林尼治标准​​时间01月24日
服务器:WSGIServer / 0.1的Python / 2.7.3
最后修饰:周六,2015年1月24日18时45分52秒格林尼治标准​​时间
内容长度:9476
内容类型:应用程序/ JavaScript的
 

这是谷歌回应:

 访问控制 - 允许 - 产地:*
备用协议:443:QUIC,P = 0.02
缓存控制:公共,最大年龄= 31536000
内容编码:gzip
内容长度:29524
内容类型:文本/ javascript的;字符集= UTF-8
日期:星期六,2015年22时57分38秒格林尼治标准​​时间01月24日
到期:太阳,2016年1月24日22点57分38秒格林尼治标准​​时间
最后一次修改:周五,2014年12月19日15点56分50秒GMT
服务器:sffe
状态:200 OK
时序允许原产地:*
有所不同:接受编码
版本:HTTP / 1.1
的X内容类型选项:nosniff
的X XSS防护:1;模式=块
 

解决方案

在经历了所有我能想到的,
我终于发现了问题。
这是dajaxice 。这显然​​是压倒一切的一些事情在jQuery的。照片 评论出来后,我能得到正常的行为。

我发现这个问题可以使用jQuery的人来解决,加入 DAJAXICE_XMLHTT preQUEST_JS_IMPORT =假在你的settings.py

I'm trying to get a static file (a js script) from django (development server) using jquery ajax but something is preventing the $.ajax promise to resolve.

Here's my ajax call :

    $.when(
        $.ajax({dataType: "script", cache: true, url: "/static/ajax_upload/js/jquery.iframe-transport.js"}),
        $.ajax({dataType: "script", cache: true, url: "/static/ajax_upload/js/ajax-upload-widget.js"})
    ).then(function() {
        console.log( "Load was performed." );
        AjaxUploadWidget.autoDiscover();
        $("#level-modal").foundation("reveal", "open");
    }, function () { alert("An error occured, try reloading the page."); });

Nothing in the then function is called, success nor failure.

The weird thing is that the same ajax call with files from google works :

    $.when(
        $.ajax({dataType: "script", cache: true, url: "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"}),
        $.ajax({dataType: "script", cache: true, url: "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"})
    ).then(function() {
        console.log( "Load was performed." );
        AjaxUploadWidget.autoDiscover();
        $("#level-modal").foundation("reveal", "open");
    }, function () { alert("An error occured, try reloading the page."); });

I think something might be wrong with my responses headers but I get a 200 status and I ain't an expert in Http.

Response from django :

HTTP/1.0 200 OK
Date: Sat, 24 Jan 2015 22:57:35 GMT
Server: WSGIServer/0.1 Python/2.7.3
Last-Modified: Sat, 24 Jan 2015 18:45:52 GMT
Content-Length: 9476
Content-Type: application/javascript

Response from google :

access-control-allow-origin:*
alternate-protocol:443:quic,p=0.02
cache-control:public, max-age=31536000
content-encoding:gzip
content-length:29524
content-type:text/javascript; charset=UTF-8
date:Sat, 24 Jan 2015 22:57:38 GMT
expires:Sun, 24 Jan 2016 22:57:38 GMT
last-modified:Fri, 19 Dec 2014 15:56:50 GMT
server:sffe
status:200 OK
timing-allow-origin:*
vary:Accept-Encoding
version:HTTP/1.1
x-content-type-options:nosniff
x-xss-protection:1; mode=block

解决方案

After going through everything I could think of,
I finally found the problem.
It was dajaxice. It is apparently overriding some things in jquery.
After commenting it out, I was able to get the normal behavior.

I found out that the problem can be resolved for people using jquery by adding DAJAXICE_XMLHTTPREQUEST_JS_IMPORT = False in your settings.py

这篇关于$。阿贾克斯()Django的失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆