2017-05-08 143 views
1

我控制器回報:閱讀JSON行

@http.route('/my_json', type="json", auth="public") 
    def some_json(self): 
     return json.dumps({"ids":[{"id": 1,"name": "Audi"},{"id": 2,"name": "BMW"},{"id": 3,"name": "OPEL"}]}) 

如何在DIV負載數據,例如新的HTML頁面。

1奧迪

2 BMW

3歐寶

功能callJson(){

$.ajax({ 
    type: "POST", 
    url: "/test_json", 
    async: false, 
    data: JSON.stringify({}), 
    contentType: "application/json", 
    complete: function (data) { 
      alert(data["responseText"]) 
    }, 
    error: function() { 
      alert("Error") 
      } 
    }); 

警報返回:

{ 「jsonrpc」: 「2.0」, 「id」:null,「result」:「{\」ids \「:[{\」id \「:1,\」name \「:\」Audi \「},{\」id \ \「name \」:\「BMW \」},{\「 ID \ 「:3,\」 名稱\ 「:\」 歐寶\ 「}]}」

回答

0

閱讀這個答案與評論here一起,你可以使用

JSON.stringify(results['ids'][i]['id'])

0

在你的情況下,你不需要使用json.dumps。

在odoo中,您有兩種可能的路線類型。

  1. HTML。

該方法必須返回一個字符串或渲染方法

@http.route('/my_html_render', type="html", auth="public") 
def html_render(self): 
    value_dict = {'a':'hello'} 
    return request.render('template_name',value_dict) 

@http.route('/my_html_string', type="html", auth="public") 
def html_string(self): 
    return "Hello world" 
  • JSON
  • Odoo將JSON格式轉換您字典。

    @http.route('/my_json', type="json", auth="public") 
    def some_json(self): 
        return {"ids":[{"id": 1,"name": "Audi"},{"id": 2,"name": "BMW"},{"id": 3,"name": "OPEL"}]}