Google App Engine和jQuery Ajax导致Broken Pipe错误

时间:2022-08-22 15:39:41

I have a pretty standard case, where I try to submit some JSON data via jQuery's Ajax.

我有一个非常标准的案例,我尝试通过jQuery的Ajax提交一些JSON数据。

My Java Script code looks like this:

我的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>

On the App Engine side I have this:

在App Engine方面我有这个:

class Submit(webapp.RequestHandler):
    def post(self):
        logging.info(self.request.body)
        self.response.out.write("test_response")

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:

我完全收到了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

I have absolutely no idea what I do wrong, since this seems straightforward.

我完全不知道我做错了什么,因为这看起来很简单。

3 个解决方案

#1


4  

My guess is that you need to cancel the submit action by calling .preventDefault() on the event object or by returning false from your handler. When the default submit behavior fires it navigates the browser away from the page.

我的猜测是你需要通过在事件对象上调用.preventDefault()或从你的处理程序返回false来取消提交操作。当默认提交行为触发时,它会导航浏览器远离页面。

#2


2  

The solution was quite simple, although it took me a while to figure it out.

解决方案很简单,虽然我花了一段时间才弄明白。

In the submit button <input type="submit" id="submit" value="Submit"> you cannot use type="submit".

在提交按钮中,您不能使用type =“submit”。

I changed it to type="button" and since then it works perfectly.

我把它改成了type =“button”,从那时起它就完美了。

If anyone knows why that is, feel free to enlighten me. Thanks.

如果有人知道为什么,请随时赐教。谢谢。

#3


2  

This is because the dev_appserver is single-threaded and it runs in a single process in your dev machine therefore it cannot handle two requests at the same time.

这是因为dev_appserver是单线程的,它在您的开发机器中的单个进程中运行,因此它无法同时处理两个请求。

As John pointed out, and as you already discovered, you were sending two simultaneous requests to the dev_server breaking the pipe.

正如John指出的那样,正如您已经发现的那样,您向dev_server发送了两个同时发出的请求,从而破坏了管道。

#1


4  

My guess is that you need to cancel the submit action by calling .preventDefault() on the event object or by returning false from your handler. When the default submit behavior fires it navigates the browser away from the page.

我的猜测是你需要通过在事件对象上调用.preventDefault()或从你的处理程序返回false来取消提交操作。当默认提交行为触发时,它会导航浏览器远离页面。

#2


2  

The solution was quite simple, although it took me a while to figure it out.

解决方案很简单,虽然我花了一段时间才弄明白。

In the submit button <input type="submit" id="submit" value="Submit"> you cannot use type="submit".

在提交按钮中,您不能使用type =“submit”。

I changed it to type="button" and since then it works perfectly.

我把它改成了type =“button”,从那时起它就完美了。

If anyone knows why that is, feel free to enlighten me. Thanks.

如果有人知道为什么,请随时赐教。谢谢。

#3


2  

This is because the dev_appserver is single-threaded and it runs in a single process in your dev machine therefore it cannot handle two requests at the same time.

这是因为dev_appserver是单线程的,它在您的开发机器中的单个进程中运行,因此它无法同时处理两个请求。

As John pointed out, and as you already discovered, you were sending two simultaneous requests to the dev_server breaking the pipe.

正如John指出的那样,正如您已经发现的那样,您向dev_server发送了两个同时发出的请求,从而破坏了管道。