阅读JSON行

问题描述:

我控制器回报:阅读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,\” 名称\ “:\” 欧宝\ “}]}”

阅读这个答案与评论here一起,你可以使用

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

在你的情况下,你不需要使用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" 
  1. JSON
  2. 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"}]} 
    
开始=>