2012-08-30 50 views
14

在燒瓶中的應用程序,我有以下的Ajax調用:瓶的Python,試圖列表或字典返回Ajax調用

$.ajax({ 
      url: "{{ url_for('bookings.get_customer') }}", 
      type: "POST", 
      data: nameArray, 
      success: function(resp){ 
       console.log(resp) 
      } 
     }) 

正如你所看到的,我傳遞一個數組反對,我將尋找我mongo數據庫,它將返回或不返回客戶。

所以,蟒蛇高清,負責處理這個Ajax調用:

@bookings.route('/get_customer', methods=[ 'POST' ]) 
def get_customer(): 
    name = {} 
    for key, value in request.form.items(): 
    name[ key ] = value 

    customer_obj = customer_class.Customer() 
    results = customer_obj.search_customer(name) 

    return results  

爲了便於討論,讓說的customer_obj調用返回下面的列表:

[{'customer': { 
       u'first_name': u'Dave', 
       u'tel': u'0121212121458', 
       u'country': u'UK',  
       u'address2': u'Townington', 
       u'address3': u'Cityville', 
       u'email': u'[email protected]', 
       u'postcode': u'A10 5BC', 
       u'address1': u'10 High Street', 
       u'second_name': u'Smith' 
      }, 
'customer_id': u'DaveSmithA10 5BCCat_Vegas1346244086' 
}] 

當我嘗試將其返回給ajax調用爲

return results 

我收到以下錯誤:

TypeError: 'list' object is not callable 

這裏是回溯:

Traceback (most recent call last): 
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1701, in __call__ 
return self.wsgi_app(environ, start_response) 
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1689, in wsgi_app 
response = self.make_response(self.handle_exception(e)) 
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1687, in wsgi_app 
response = self.full_dispatch_request() 
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1361, in 
full_dispatch_request 
response = self.make_response(rv) 
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1450, in make_response 
rv = self.response_class.force_type(rv, request.environ) 
File "/usr/local/lib/python2.7/dist-packages/werkzeug/wrappers.py", line 711, in 
force_type 
response = BaseResponse(*_run_wsgi_app(response, environ)) 
File "/usr/local/lib/python2.7/dist-packages/werkzeug/test.py", line 818, in 
run_wsgi_app 
app_iter = app(environ, start_response) 
TypeError: 'list' object is not callable 

有沒有人有什麼建議?

感謝

+0

一切都很好,有凹痕嗎?看起來像'返回結果'是不是在你的代碼中的正確位置... –

+0

嗨,是的,抱歉,這是我的一個錯字。我現在糾正了它。 – Ste77

回答

29

瓶不指望你會從您的視圖函數返回list對象。之前嘗試jsonify它:

from flask import jsonify 

@bookings.route('/get_customer', methods=[ 'POST' ]) 
def get_customer(): 
    name = {} 
    for key, value in request.form.items(): 
     name[ key ] = value 

    customer_obj = customer_class.Customer() 
    results = customer_obj.search_customer(name) 

    return jsonify(customers=results)  
+0

是的,很抱歉,我一直在嘗試發送字典和列表,以查看它是否有所作爲。我已經更新了我的問題以保持一致,我迷失在細節:)。我會立即嘗試你的建議。 – Ste77

+0

更新了我的答案。 –

+0

你是一個學者和紳士,它的工作原理。我不得不返回一個字典,而不是一個列表 - 返回jsonify(**結果[0]),這不正是我想要的,但我可以輕鬆地找到解決方法。謝謝。 – Ste77

5

你需要做的

return jsonify(result=your_result) 

還要檢查documentation這可能是真的很有幫助。

+0

感謝您的鏈接。 – Ste77

8

josonify works ..但如果你打算只傳遞一個沒有'結果'鍵的數組,你可以使用python的json庫。以下轉換適用於我..

import json 

@app.route('/test/json') 
def test_json(): 
list = [ 
     {'a': 1, 'b': 2}, 
     {'a': 5, 'b': 10} 
     ] 
return json.dumps(list) 
+1

這種方法的一個問題是Flask會發送錯誤的添加「Content-Type」頭文件。它將被設置爲'text/html; charset = utf-8',而不是語義上正確的'application/json' –