我有一个pretty的标准情况下,如果我尝试通过jQuery的Ajax提交一些JSON数据。
I have a pretty standard case, where I try to submit some JSON data via jQuery's Ajax.
我的Java脚本code是这样的:
My Java Script code looks like this:
<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的身边,我有这样的:
On the App Engine side I have this:
class Submit(webapp.RequestHandler):
def post(self):
logging.info(self.request.body)
self.response.out.write("test_response")
我收到JSON数据 logging.info(self.request.body)
完美的,但后来只要我送出去的响应会触发一个错误。错误日志我得到的是这样的:
I receive the JSON data logging.info(self.request.body)
perfectly, but then it triggers an error as soon as I send out the response. The error log I get is the following:
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
我绝对不知道我做错了,因为这看起来非常简单。
I have absolutely no idea what I do wrong, since this seems straightforward.
解决方案是很简单,但我花了一段时间来弄明白。
The solution was quite simple, although it took me a while to figure it out.
在提交按钮
&LT;输入类型=提交ID =提交值=提交&GT;
则不能使用 TYPE =提交
。
In the submit button
<input type="submit" id="submit" value="Submit">
you cannot use type="submit"
.
我把它改为 TYPE =按钮
从那以后,它完美的作品。
I changed it to type="button"
and since then it works perfectly.
如果有谁知道这是为什么,请随时赐教。谢谢你。
If anyone knows why that is, feel free to enlighten me. Thanks.