I am working on a Django project. All went well till I created an Ajax request to send values from the html page to the backend (views.py).
When I send the data using Ajax, I am able to view the values being passed to views.py, and it even reaches the render_to_response method and displays my page, but throws the broken pipe error in the terminal. I don't see any kind of disruption to the program, but I wanted to know if there is a way to prevent this error from occurring. I checked the other responses. But no luck so far.
When I try to hit submit again on the refreshed page, I get this message:
The page that you're looking for used information that you entered. Returning to that page might cause any action you took to be repeated. Do you want to continue? [Submit] [Cancel]`
Here is the dump:
Traceback (most recent call last):
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 34812)
----------------------------------------
File "/usr/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 284, in run
self.finish_response()
File "/usr/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 324, in finish_response
self.write(data)
File "/usr/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 403, in write
self.send_headers()
File "/usr/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 467, in send_headers
self.send_preamble()
File "/usr/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 385, in send_preamble
'Date: %s\r\n' % http_date()
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
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 284, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 310, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 323, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 570, in __init__
BaseHTTPRequestHandler.__init__(self, *args, **kwargs)
File "/usr/lib/python2.7/SocketServer.py", line 640, in __init__
self.finish()
File "/usr/lib/python2.7/SocketServer.py", line 693, 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
Update: Here is the code that I am sending:
$( document ).ready(function() {
$.csrftoken();
$("#submitdata").click(function(){
//values = [tmode, fmode, t_cool, t_heat, hold];
values = {
"tmode": tmode,
"fmode": fmode,
"t_cool": t_cool,
"t_heat": t_heat,
"hold": hold
};
var jsonText = JSON.stringify(values);
$.ajax({
url: "/submitdata/",
type: 'POST',
data: jsonText,
dataType: 'json',
success:function(data){
console.log(data.success);
},
complete:function(){
console.log('complete');
},
error:function (xhr, textStatus, thrownError){
console.log(thrownError);
console.log(obj);
}
});
});
});
And here is my views.py:
@login_required
def submitvalues(request):
#context = RequestContext(request)
if request.POST:
jsonvalues = json.loads(request.raw_post_data)
print jsonvalues
return HttpResponse(json.dumps(dict(status='updated')), mimetype="application/json")
I am still facing the same issue. Can someone help me with this?
Edit on 5/28/2014: I just figured out the reason for a Broken Pipe. It was because I was not sending back the response from Python and was just expecting the page to refresh automatically. I am a newbie to all of this, and took me a while to figure out why this happened.
You haven't posted any code, but this is probably because you have triggered the Ajax request on a button submit but haven't prevented the default action. So the Ajax request is made, but by the time it comes to return the data, the browser has already requested the next page anyway, so there is nothing to receive it.
I have solved this problem by adding this:
self.send_header("Access-Control-Allow-Origin", "*")
Because I found some error on sending post request page:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is there
Then I got this solution and solved above problem.
来源:https://stackoverflow.com/questions/22560259/error-errno-32-broken-pipe