问题
I am create controller in OpenERP Framework. Following is my code and i set http.route type="http"
,
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>"
Above code work perfect once i login into openerp after i modify URL http://localhost:8069/demo_html
show me return result This is a test
in h1 heading tag.
But same way i try to type="json"
and add following json code and again try to call URL http://localhost:8069/demo_json
Its not work properly and show me error "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"}
So my question is how to route json. Any help would be appreciate Thank you.
回答1:
This is because there is difference between type="json"
and 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.
回答2:
I think , you need to do some extra stuff while working with type="json",you have to trigger that method using json rpc from js.
like :
$(document).ready(function () {
openerp.jsonRpc("demo_json", 'call', {})
.then(function (data) {
$('body').append(data[0]);
});
return;
})
and yes do not forget to return your dictionary in list like
@http.route('demo_json', type="json")
def some_json(self):
return [{"sample_dictionary": "This is a sample JSON dictionary"}]
来源:https://stackoverflow.com/questions/22776884/openerp-http-routedemo-json-type-json-url-not-displaying-json-data