2

I am making a simple API call in a Django application but I'm having some issues

Here is the ajax call:

$.ajax({
    type: "GET",
    url: "/query/",
    success: function (data) {
        alert(data);
    }
});

Here is the function calling the API:

def query(request):
    response = requests.get("http://api.open-notify.org/iss-now.json")
    return JsonResponse(response.status_code, safe=False)

This only works every other time it is triggered, one failure then one success. There is no exception captured when it fails. Below is what is displayed in the output window after a failure.

The thread 0x2008 has exited with code 0 (0x0).
File "C:\Program Files\Python36\lib\wsgiref\handlers.py", line 138, in run
self.finish_response()
File "C:\Program Files\Python36\lib\wsgiref\handlers.py", line 180, in 
finish_response
self.write(data)
File "C:\Program Files\Python36\lib\wsgiref\handlers.py", line 274, in write
self.send_headers()
File "C:\Program Files\Python36\lib\wsgiref\handlers.py", line 332, in 
send_headers
self.send_preamble()
File "C:\Program Files\Python36\lib\wsgiref\handlers.py", line 255, in 
send_preamble
('Date: %s\r\n' % format_date_time(time.time())).encode('iso-8859-1')
File "C:\Program Files\Python36\lib\wsgiref\handlers.py", line 453, in 
_write
result = self.stdout.write(data)
File "C:\Program Files\Python36\lib\socketserver.py", line 775, in write
self._sock.sendall(b)
ConnectionAbortedError: [WinError 10053] An established connection was 
aborted by the software in your host machine
[27/Jul/2017 13:56:15] "GET /query/ HTTP/1.1" 500 59

Exception happened during processing of request from ('127.0.0.1', 56126)
Traceback (most recent call last):
File "C:\Program Files\Python36\lib\wsgiref\handlers.py", line 138, in run
self.finish_response()
File "C:\Program Files\Python36\lib\wsgiref\handlers.py", line 180, in 
finish_response
self.write(data)
File "C:\Program Files\Python36\lib\wsgiref\handlers.py", line 274, in write
self.send_headers()
File "C:\Program Files\Python36\lib\wsgiref\handlers.py", line 332, in 
send_headers
self.send_preamble()
File "C:\Program Files\Python36\lib\wsgiref\handlers.py", line 255, in 
send_preamble
('Date: %s\r\n' % format_date_time(time.time())).encode('iso-8859-1')
File "C:\Program Files\Python36\lib\wsgiref\handlers.py", line 453, in 
_write
result = self.stdout.write(data)
File "C:\Program Files\Python36\lib\socketserver.py", line 775, in write
self._sock.sendall(b)
ConnectionAbortedError: [WinError 10053] An established connection was 
aborted by the software in your host machine

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Program Files\Python36\lib\wsgiref\handlers.py", line 141, in run
self.handle_error()
File "D:\Dropbox\Django Projects\DjangoMOA\environment\lib\site-
packages\django\core\servers\basehttp.py", line 88, in handle_error
super(ServerHandler, self).handle_error()
File "C:\Program Files\Python36\lib\wsgiref\handlers.py", line 368, in 
handle_error
self.finish_response()
File "C:\Program Files\Python36\lib\wsgiref\handlers.py", line 180, in 
finish_response
self.write(data)
File "C:\Program Files\Python36\lib\wsgiref\handlers.py", line 274, in write
self.send_headers()
File "C:\Program Files\Python36\lib\wsgiref\handlers.py", line 331, in 
send_headers
if not self.origin_server or self.client_is_modern():
File "C:\Program Files\Python36\lib\wsgiref\handlers.py", line 344, in 
client_is_modern
return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9'
TypeError: 'NoneType' object is not subscriptable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Program Files\Python36\lib\socketserver.py", line 639, in 
process_request_thread
self.finish_request(request, client_address)
File "C:\Program Files\Python36\lib\socketserver.py", line 361, in 
finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Program Files\Python36\lib\socketserver.py", line 696, in __init__
self.handle()
File "D:\Dropbox\Django Projects\DjangoMOA\environment\lib\site-
packages\django\core\servers\basehttp.py", line 155, in handle
handler.run(self.server.get_app())
File "C:\Program Files\Python36\lib\wsgiref\handlers.py", line 144, in run
self.close()
File "C:\Program Files\Python36\lib\wsgiref\simple_server.py", line 35, in 
close
self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'

The thread 0x420 has exited with code 0 (0x0).

I have tested the URL in postman and received a response every time with no issues.

Any advice or thoughts would be gratefully appreciated.

1
  • try response.['status_code'] Commented Jul 27, 2017 at 18:03

1 Answer 1

2

Manged to get to the bottom of it. Looks like the page was reloading and causing the "ConnectionAbortedError".

I had the call nested within a .submit function which required an

event.preventDefault();

to be added on the end. full working code now looks like this.

$("#form").submit(
    function () {
        $.ajax({
            type: "GET",
            url: "/query/",
            success: function (data) {
                alert(data);
            }
        });
        event.preventDefault();
    }

);

Hope this helps someone else down the line :)

Sign up to request clarification or add additional context in comments.

1 Comment

I'm not sure how long it would have taken me to look at the JavaScript for this issue, I'd have swore this was coming from Python. This was EXACTLY what I needed for the exact same problem!!! Thanks for coming back and posting your answer/fix! I scratched my head for hours on this one!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.