2015-09-02 61 views
0

我是Jquery的新手,并尝试轮询变量状态更改的服务器API。事情很好,除非我在使用“return redirect(url_for ...)”作为返回类型时出现“断开的管道”。瓶“错误:[Errno 32]当使用”返回重定向(url_for ...)“时,损坏的管道”

我该怎么做呢?我只想在变量发生变化时触发主视图,因为计算该视图的对象是昂贵的,如果状态相同则不需要。

Python代码:

@app.route('/status') 
def check_deployment_status(): 

    # get all the simpleDB domains 
    # the list of clusters are used to build the left nav 
    clusters = sdb_conn.get_domain('clusters') 

    cluster_id = request.args.get('id', '') 
    state = request.args.get('state', '') 

    # get that cluster from the list of clusters 
    cluster=clusters.get_item(cluster_id) 

    print "cluster state: " + cluster['state'] 
    print "dashboard state: " + state 

    if cluster['state'] != state: 
     return redirect(url_for('cluster', id=cluster_id)) 
    else: 
     return 'this' 

jQuery代码:

$(document).ready(function() { 
    (function poll() { 
     setTimeout(function() { 
     $.ajax({ 
      url: "/status", 
      type: "GET", 
      success: function(data) { 
       console.log("polling"); 
      }, 
      dataType: "json", 
      data: {"id": $('#id').text(), "state": $('#state').text()}, 
      complete: poll, 
      timeout: 2000 
     }) 
     }, 5000); 
    })(); 
}); 

错误,忘了发布错误:

Exception happened during processing of request from ('127.0.0.1', 56862) 
Traceback (most recent call last): 
    File "/usr/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock 
self.process_request(request, client_address) 
    File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request 
self.finish_request(request, client_address) 
    File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request 
self.RequestHandlerClass(request, client_address, self) 
    File "/usr/lib/python2.7/SocketServer.py", line 651, in __init__ 
self.finish() 
    File "/usr/lib/python2.7/SocketServer.py", line 710, in finish 
self.wfile.close() 
    File "/usr/lib/python2.7/socket.py", line 279, in close 
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 

回答

-1

是啊,我要对这个错误的方式(我认为)。最后,我制作一个URL并将其作为jQuery响应传回,并在JavaScript中处理重定向。新的jQuery函数如下:

$(document).ready(function() { 
(function poll() { 
    setTimeout(function() { 
    $.ajax({ 
     url: "/status", 
     type: "GET", 
     success: function(data) { 
     if (data.redirect) { 
      // data.redirect contains the string URL to redirect to 
      window.location.href = data.redirect; 
     } 
      console.log(data.redirect); 
     }, 
     dataType: "json", 
     data: {"id": $('#id').text(), "state": $('#state').text()}, 
     complete: poll, 
     timeout: 2000 
    }) 
    }, 5000); 
})(); 
}); 

而且蟒,像这样:

@app.route('/status') 
def check_deployment_status(): 

    # get all the simpleDB domains 
    # the list of clusters are used to build the left nav 
    clusters = sdb_conn.get_domain('clusters') 

    cluster_id = request.args.get('id', '') 
    state = request.args.get('state', '') 

    # get that cluster from the list of clusters 
    cluster=clusters.get_item(cluster_id) 

    print "cluster state: " + cluster['state'] 
    print "dashboard state: " + state 

    if cluster['state'] != state: 
     #return redirect(url_for('cluster', id=cluster_id)) 
     return jsonify(redirect=url_for('cluster', id=cluster_id)) 
    else: 
     return 'this' 
相关问题