2
我已經開發了一個Django的Web應用程序。它使用AJAX調用(使用jQuery的$.ajax
)根據某些輸入文本更新圖像。下面的代碼是用於該目的:Django異常,同時進行AJAX調用
$('img#diagram').attr('src', '/images/ajax/ajax_loader.gif');
$.ajax({
type: 'POST',
url: '/draw/',
data: { 'diagram': $('#diagram-text').val() },
cache: false,
success: function(mesg, txtStatus, XmlHttpRequest) {
result = $.parseJSON(mesg);
if (result['error']) {
alert('An error was encountered: ' + result['error']);
}
else {
$('img#diagram').attr('src', result['diagram_url']);
}
},
error: function(XmlHttpRequet, txtStatus, errorThrown) {
alert('Failed to draw the diagram!\n' + errorThrown);
},
dataType: 'html'
});
它可以正常工作,即輸入被改變,「繪製」按鈕被點擊後更新圖像。
問題是,如果我沒有輸入任何東西,並點擊'繪製'按鈕,AJAX加載器圖像仍然存在。現在,什麼發生,如果我請執行下列操作之一:
- 輸入一些文字,然後單擊「繪圖」按鈕
- 刷新頁面
事實上,頁面沒有按」 t重新加載。
如果我現在停下Django開發服務器,我收到以下錯誤:
Exception happened during processing of request from ('127.0.0.1', 44166)
Unhandled exception in thread started by <function inner_run at 0x9f6d5dc>
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python2.6/dist-packages/apport_python_hook.py", line 48, in apport_excepthook
if not enabled():
TypeError: 'NoneType' object is not callable
Original exception was:
Traceback (most recent call last):
File "/usr/local/lib/python2.6/dist-packages/django/core/management/commands/runserver.py", line 60, in inner_run
run(addr, int(port), handler)
File "/usr/local/lib/python2.6/dist-packages/django/core/servers/basehttp.py", line 721, in run
httpd.serve_forever()
File "/usr/lib/python2.6/SocketServer.py", line 226, in serve_forever
self._handle_request_noblock()
File "/usr/lib/python2.6/SocketServer.py", line 283, in _handle_request_noblock
self.handle_error(request, client_address)
File "/usr/lib/python2.6/SocketServer.py", line 335, in handle_error
import traceback
ImportError: No module named traceback
任何人都可以提出什麼嗎?當然,我可以通過驗證來防止空字符串。但是爲什麼這個確實發生,因爲AJAX是異步的?