【问题标题】:Read json line by line odoo and ajax逐行读取 json odoo 和 ajax
【发布时间】:2017-05-08 11:36:10
【问题描述】:

我的控制器返回:

@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 宝马

3 欧宝

函数调用Json() {

$.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\": 2, \"name\": \"BMW\"}, {\"id\": 3, \"name\": \"Opel\"}]}"}

【问题讨论】:

    标签: json ajax openerp odoo-9 odoo-10


    【解决方案1】:

    与 cmets here 一起阅读此答案,您可以使用

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

    【讨论】:

      【解决方案2】:

      在您的情况下,您不需要使用 json.dumps。

      在 odoo 中,您有 2 种可能的路线类型。

      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

      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"}]}
      

      【讨论】:

        猜你喜欢
        • 2014-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-16
        • 2010-11-07
        • 1970-01-01
        相关资源
        最近更新 更多