我有一个非常标准的案例,我尝试通过jQuery的Ajax提交一些JSON数据。
我的Java Script代码如下所示:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<script type='text/javascript' src='script.js'></script>
<script type='text/javascript'>
$(document).ready(function() {
$("#submit").click(function() {
$.post('/test', {test : '123'}, function(data) { alert("callback received"); }, 'json');
});
});
</script>
在App Engine方面我有这个:
class Submit(webapp.RequestHandler):
def post(self):
logging.info(self.request.body)
self.response.out.write("test_response")
我完全收到了JSON数据logging.info(self.request.body)
,但是一发出响应就会触发错误。我得到的错误日志如下:
Exception happened during processing of request from ('192.168.2.8', 38875)
Traceback (most recent call last):
File "/usr/lib/python2.6/SocketServer.py", line 283, in handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python2.6/SocketServer.py", line 309, in process_request
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 "/home/hw/Siine/google_appengine/google/appengine/tools/dev_appserver.py", line 3123, in init_
BaseHTTPServer.BaseHTTPRequestHandler.__init__(self, *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
我完全不知道我做错了什么,因为这看起来很简单。
答案 0 :(得分:4)
我的猜测是你需要通过调用事件对象上的.preventDefault()
或从处理程序返回false来取消提交操作。当默认提交行为触发时,它会将浏览器导航离开页面。
答案 1 :(得分:2)
解决方案很简单,虽然我花了一些时间来弄明白。
在提交按钮中
<input type="submit" id="submit" value="Submit">
您无法使用type="submit"
。
我将其更改为type="button"
,从那时起它就完美无缺。
如果有人知道为什么会这样,请随时赐教。感谢。
答案 2 :(得分:2)
这是因为dev_appserver是单线程的,它在dev机器的单个进程中运行,因此它无法同时处理两个请求。
正如John指出的那样,正如您已经发现的那样,您向dev_server发送了两个同时发出请求的请求。