0
我試着設置我的表單按鈕來保存表單而不重新加載頁面。Django - 發送數據時Ajax表單失敗
一切都看起來正確;但我想我錯過了一些東西。這裏是我的代碼:
AJAX部分:
$('#submit').click(function() {
var title = $('#id_title').val();
var body = $('#id_body').val();
var isdraft = $('#id_isdraft').is(':checked');
var ajaxOptions = {
type: 'post',
url: '/admin/post/save/',
data: {
'title': title,
'body': body,
'isdraft': isdraft
},
success: function(){
alert("save success");
},
error: function(){
alert("fail");
}
};
$.ajax(ajaxOptions);
});
意見部分:
@require_POST
def save_post(request):
title = request.POST['title']
body = request.POST['body']
isdraft = request.POST['isdraft']
owner = request.user
post = Post(title=title, body=body, isdraft=isdraft, owner=owner)
post.save()
return HttpResponse(200)
URL部分:
url(r'^admin/post/save/', view='save_post', name='save_post'),
當I C舔提交按鈕;調用ajax錯誤函數。
謝謝
編輯:
在Firefox;它將數據保存到數據庫中。它顯示錯誤,但再次保存。
但在控制檯[runserver lines];有一個錯誤:
Exception happened during processing of request from ('10.10.10.1', 42526)
Traceback (most recent call last):
File "/usr/lib/python2.6/SocketServer.py", line 560, in process_request_thread
self.finish_request(request, client_address)
File "/usr/lib/python2.6/SocketServer.py", line 322, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/opt/sosyate_/sosyate_env/lib/python2.6/site-packages/django/core/servers/basehttp.py", line 139, in __init__
super(WSGIRequestHandler, self).__init__(*args, **kwargs)
File "/usr/lib/python2.6/SocketServer.py", line 618, in __init__
self.finish()
File "/usr/lib/python2.6/SocketServer.py", line 661, in finish
self.wfile.flush()
File "/usr/lib/python2.6/socket.py", line 297, in flush
self._sock.sendall(buffer(data, write_offset, buffer_size))
error: [Errno 32] Broken pipe
但在鉻;沒有像這樣的錯誤。它只是拋出我在ajax函數下定義的錯誤。
請修復視圖文件中的格式。在'views.save_post()'什麼是'Post'?嘗試使用瀏覽器開發人員工具來查看對Ajax調用的響應 - 這可能包含更多信息。 – scytale
我不確定;但它會拋出403錯誤。 – alix
不確定什麼? 403是Access Forbidden - 你看到哪裏? – scytale