Django开发服务器不从Ajax的请求发球失误 [英] Django Development Server Not Serving Errors from Ajax Requests
问题描述
我一直试图让一些Ajax的东西在我的Django应用程序的工作,但一直很困难,因为由于某种原因,语法和类型的错误不会导致服务器崩溃,并默默地失败。这怎么可能?
的JavaScript(jQuery的):
add_foo =功能(){
VAR numfoos = $(#DIV&foos_container GT; div.foo)的长度+ 1。
VAR foo_id = numfoos +-foo;
VAR股利= $(< DIV>< / DIV>中)。ATTR(ID,foo_id).addClass(富);
div.load(HTTP://本地主机:8000 /阿贾克斯/获取/ URL /);
div.appendTo(#DIV foos_container);
};
Python的:
高清ajax_add_foo(要求):
打印的request.session ['editing_foos']。键()
键=的request.session ['editing_foos']。键()
如果len(键)== 0:
next_key = 1
其他:
next_key = MAX([id_from_ preFIX(键)键在键])
打印next_key
表= FooForm(preFIX = next_key)
打印next_key
。的request.session ['editing_foos']更新({CREATE_ preFIX(FooForm,next_key):-1})#这foo是新的,并没有PKEY
打印的request.session ['editing_foos']
返回render_to_response('酒吧/ foo_fragment.html',
{'fooform:fooform,},
context_instance = RequestContext的(要求))
这可能是因为服务器返回的错误code(500)和你的jQuery的鳕鱼不会对错误做任何事情。您可以张贴$不用彷徨或.post的$ code使用的是?
编辑:的如果你使用的是$ .load,有一个回调函数的地方,你可以创建显示错误。它是pretty的简单,你需要定义一个类似的功能:
函数(responseText的,textStatus,XMLHtt prequest){
如果(textStatus< 200 || textStatus> = 299){
$(文件)。html的(responseText的);
}
}
这样,你就会把错误信息到整个文件,看看它。我并不确切地知道,如果以上的作品,因为我无法测试它,但你应该能够建立这个东西的工作。
I've been trying to get some Ajax stuff to work in my Django application, but it has been difficult because for some reason syntax and type errors are not causing a server crash and are failing silently. How could this be?
JavaScript (jQuery):
add_foo = function() {
var numfoos = $("div#foos_container > div.foo").length + 1;
var foo_id = numfoos + "-foo";
var div = $("<div></div>").attr("id",foo_id).addClass("foo");
div.load("http://localhost:8000/ajax/get/url/");
div.appendTo("div#foos_container");
};
Python:
def ajax_add_foo(request):
print request.session['editing_foos'].keys()
keys = request.session['editing_foos'].keys()
if len(keys) == 0:
next_key = 1
else:
next_key = max([ id_from_prefix(key) for key in keys ])
print next_key
form = FooForm(prefix=next_key)
print next_key
request.session['editing_foos'].update( {create_prefix(FooForm, next_key) : -1 } ) # This foo is new and has no pkey
print request.session['editing_foos']
return render_to_response( 'bar/foo_fragment.html',
{'fooform' : fooform, },
context_instance=RequestContext(request))
This is probably because server returns error code (500) and your jQuery cod doesn't do anything on error. Could you post $.get or $.post code you are using?
EDIT: If you are using $.load, there is a place for a callback function, that you can create to display errors. It's pretty simple, you need to define a function similar to this:
function (responseText, textStatus, XMLHttpRequest){
if (textStatus < 200 || textStatus >= 299){
$(document).html(responseText);
}
}
This way you will put error message into the whole document and see it. I don't exactly know, if the above works, because I can't test it, but you should be able to construct something working on this.
这篇关于Django开发服务器不从Ajax的请求发球失误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!