Django应用程序

时间:2017-07-27 13:17:08

标签: python ajax django

我正在Django应用程序中进行简单的API调用,但我遇到了一些问题

这是ajax调用:

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

以下是调用API的函数:

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

这只会在每次触发时起作用,一次失败然后一次成功。失败时没有捕获异常。以下是失败后在输出窗口中显示的内容。

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).

我已经在邮递员中测试了网址,并且每次都没有问题就收到了回复。

感谢任何建议或想法。

1 个答案:

答案 0 :(得分:2)

掌握了它的底部。看起来页面正在重新加载并导致" ConnectionAbortedError"。

我将调用嵌套在.submit函数中,该函数需要

// MyViewController.swift
override func viewDidLoad() {
        super.viewDidLoad()
        let algorithm = GMUNonHierarchicalDistanceBasedAlgorithm()
        let renderer = GMUDefaultClusterRenderer(mapView: self.mapView, clusterIconGenerator: iconGenerator)
        renderer.delegate = self
        self.clusterManager = GMUClusterManager(map: self.mapView, algorithm: algorithm, renderer: renderer)

        for (index, marker) in markers.enumerated() {
            let markerItem = POIItem(position: marker.position, name: "Marker \(index)")
            self.clusterManager.add(markerItem)
        }
        self.mapViewContainer.addSubview(self.mapView)
        clusterManager.cluster()
    }

func renderer(_ renderer: GMUClusterRenderer, willRenderMarker marker: GMSMarker) {
        let pinImageView = UIImageView(image: #imageLiteral(resourceName: "pin"));
        let iconView = UIView(frame: CGRect(x: 0, y: 0, width: pinImageView.frame.width, height: pinImageView.frame.height))
        iconView.clipsToBounds = false

        iconView.backgroundColor = .clear
        iconView.addSubview(pinImageView)
        iconView.layoutIfNeeded()

        if let markerData = marker.userData {
            if let markerClusterData = (markerData as? GMUCluster) {
                let totalLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 25, height: 25))
                let labelContainerView = UIView(frame: CGRect(x: 20, y: -10, width: 25, height: 25))
                labelContainerView.backgroundColor = #colorLiteral(red: 0.9803921569, green: 0.7058823529, blue: 0.1176470588, alpha: 1)
                totalLabel.font = totalLabel.font.withSize(12.0)
                totalLabel.text = "\(markerClusterData.count)+"
                totalLabel.textAlignment = .center
                totalLabel.textColor = .white
                labelContainerView.addSubview(totalLabel)
                labelContainerView.layer.cornerRadius = 12.5
                labelContainerView.layoutIfNeeded()
                iconView.addSubview(labelContainerView)
                iconView.bounds = iconView.frame.union(labelContainerView.frame)
            }
        }

        marker.iconView = iconView
    }
// Default POIITem implementation, exactly like Google teaches.
class POIItem: NSObject, GMUClusterItem {
    var position: CLLocationCoordinate2D
    var name: String!

    init(position: CLLocationCoordinate2D, name: String) {
        self.position = position
        self.name = name
    }
}

最后添加。完整的工作代码现在看起来像这样。

event.preventDefault();

希望这有助于其他人下线:)