使用ajax发布表单时标准输出出错
使用下面的javascript代码,在发布表单时,我在StdOut中收到一个错误 我从网上得到这个函数。用于发布表格:使用ajax发布表单时标准输出出错,ajax,django,Ajax,Django,使用下面的javascript代码,在发布表单时,我在StdOut中收到一个错误 我从网上得到这个函数。用于发布表格: $(document).ajaxSend(function(event, xhr, settings) { function getCookie(name) { var cookieValue = null; if (document.cookie && document.cookie != '') {
$(document).ajaxSend(function(event, xhr, settings) {
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
function sameOrigin(url) {
// url could be relative or scheme relative or absolute
var host = document.location.host; // host + port
var protocol = document.location.protocol;
var sr_origin = '//' + host;
var origin = protocol + sr_origin;
// Allow absolute or scheme relative URLs to same origin
return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
(url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
// or any other URL that isn't scheme relative or absolute i.e relative.
!(/^(\/\/|http:|https:).*/.test(url));
}
function safeMethod(method) {
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
if (!safeMethod(settings.type) && sameOrigin(settings.url)) {
xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
}
});
HTML表单:
<form name="formCommand" id="idFormCommand" action="command/setOnline" method="post">
{% csrf_token %}
</form>
登录时出错(./manage.py runserver):
我对这个问题一无所知
执行视图代码(创建对象)。看来回报才是问题所在
我前段时间用过这个方法,作品里有一种魅力
如果我将Javascript post函数更改为
$("#idBtSetOnline").click(function(){
$.ajax({
data: $("#idFormCommand").serialize(),
type: "post",
url: "command/setOnline",
success: function(data) {
if(data == "ok")
{
alert("ok");
}
else
{
alert("ko");
}
}
});
return false;
});
它工作正常。
“return false;”语句发挥了神奇的作用:)
我还移除了
$(document).ajaxSend...
块,它也可以工作
/usr/lib/python2.7/dist-packages/django/db/models/fields/__init__.py:808: RuntimeWarning: DateTimeField received a naive datetime (2013-05-11 06:08:19.119291) while time zone support is active.
RuntimeWarning)
Traceback (most recent call last):
File "/usr/lib/python2.7/wsgiref/handlers.py", line 86, in run
self.finish_response()
File "/usr/lib/python2.7/wsgiref/handlers.py", line 127, in finish_response
self.write(data)
File "/usr/lib/python2.7/wsgiref/handlers.py", line 210, in write
self.send_headers()
File "/usr/lib/python2.7/wsgiref/handlers.py", line 268, in send_headers
self.send_preamble()
File "/usr/lib/python2.7/wsgiref/handlers.py", line 192, in send_preamble
'Date: %s\r\n' % format_date_time(time.time())
File "/usr/lib/python2.7/socket.py", line 324, in write
self.flush()
File "/usr/lib/python2.7/socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 59697)
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 593, in process_request_thread
self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 139, in __init__
super(WSGIRequestHandler, self).__init__(*args, **kwargs)
File "/usr/lib/python2.7/SocketServer.py", line 651, in __init__
self.finish()
File "/usr/lib/python2.7/SocketServer.py", line 704, in finish
self.wfile.flush()
File "/usr/lib/python2.7/socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
$("#idBtSetOnline").click(function(){
$.ajax({
data: $("#idFormCommand").serialize(),
type: "post",
url: "command/setOnline",
success: function(data) {
if(data == "ok")
{
alert("ok");
}
else
{
alert("ko");
}
}
});
return false;
});
$(document).ajaxSend...