2014-04-01 12 views
2

我在OpenERP框架中創建了控制器。以下是我的代碼,我設置http.routetype="http"OpenERP @ http.route('demo_json',type =「json」)不顯示JSON數據的URL

import openerp.http as http 
from openerp.http import request 

class MyController(http.Controller): 

    @http.route('demo_html', type="http") 
    def some_html(self): 
     return "<h1>This is a test</h1>" 

上面的代碼工作完美的一次,我登錄到的OpenERP後,我修改URL http://localhost:8069/demo_html告訴我在H1標題標籤返回結果This is a test

但是同樣我也嘗試type="json"並添加以下代碼JSON,並再次嘗試調用URL http://localhost:8069/demo_json它不能正常工作,給我錯誤"Internal Server Error"

import openerp.http as http 
from openerp.http import request 

class MyController(http.Controller): 

    @http.route('demo_html', type="http") // Work Pefrect when I call this URL 
    def some_html(self): 
     return "<h1>This is a test</h1>" 

    @http.route('demo_json', type="json") // Not working when I call this URL 
    def some_json(self): 
     return {"sample_dictionary": "This is a sample JSON dictionary"} 

所以我的問題是如何路由JSON。任何幫助將不勝感激謝謝。

回答

1

這是因爲type="json"type="http"之間存在差異。

type="json": 

it will call JSONRPC as an argument to http.route() so here , there will be only JSON data be able to pass via JSONRPC, It will only accept json data object as argument. 

type="http": 

As compred to JSON, http will pass http request arguments to http.route() not json data. 
+0

你檢查我回JSON數據返回{「sample_dictionary」:「這是一個示例JSON字典」} –

0

我想,你需要做一些額外的東西具有type =「json的」工作時,你必須使用JSON RPC從JS來觸發該方法。

like : 
$(document).ready(function() { 
    openerp.jsonRpc("demo_json", 'call', {}) 
      .then(function (data) { 
       $('body').append(data[0]); 
      }); 
    return; 
}) 

,是的,不要忘記來回報您的字典清單中像

@http.route('demo_json', type="json") 
def some_json(self): 
    return [{"sample_dictionary": "This is a sample JSON dictionary"}] 
+0

謝謝你給我答案。 jQuery代碼調用demo_json頁面頭部標籤裏面我添加。另一件事我根據我的知識JSON返回dictonary格式顯示在頁面 –

+0

是的,你是正確的JSON retunr dictonary,但它也返回字典列表,你可以檢查http://jsonlint.com/並輸入類似[{} ]並驗證。 – Heroic

+0

是是該類型列表顯示在網頁上。 –