$.ajax() 与 django 失败
$.ajax() with django fails
我正在尝试使用 jquery ajax 从 django(开发服务器)获取一个静态文件(一个 js 脚本),但是某些东西阻止了 $.ajax
承诺的解决。
这是我的 ajax 电话:
$.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."); });
then
函数中没有任何内容被调用,无论是成功还是失败。
奇怪的是,同样的 ajax 调用来自 google 的文件有效:
$.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."); });
我认为我的回复可能有问题 headers 但我的状态是 200,而且我不是 Http 方面的专家。
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
来自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
在经历了我能想到的一切之后,
终于找到问题了
是 dajaxice。它显然覆盖了 jquery.
中的某些内容
注释掉后,我能够得到正常的行为。
我发现使用 jquery 的人可以通过在 settings.py
中添加 DAJAXICE_XMLHTTPREQUEST_JS_IMPORT = False
来解决问题
我正在尝试使用 jquery ajax 从 django(开发服务器)获取一个静态文件(一个 js 脚本),但是某些东西阻止了 $.ajax
承诺的解决。
这是我的 ajax 电话:
$.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."); });
then
函数中没有任何内容被调用,无论是成功还是失败。
奇怪的是,同样的 ajax 调用来自 google 的文件有效:
$.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."); });
我认为我的回复可能有问题 headers 但我的状态是 200,而且我不是 Http 方面的专家。
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
来自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
在经历了我能想到的一切之后,
终于找到问题了
是 dajaxice。它显然覆盖了 jquery.
中的某些内容
注释掉后,我能够得到正常的行为。
我发现使用 jquery 的人可以通过在 settings.py
中添加DAJAXICE_XMLHTTPREQUEST_JS_IMPORT = False
来解决问题