Enable Dark Mode!
call-json-rpc-to-web-controller-in-odoo.png
By: Nimisha Muralidhar

How to Call JSON RPC to Web Controller in Odoo

Technical

JSON (Javascript object notation) is a data-interchange format. It is easy to read and write. It is easy to parse and generate for machines also. RPC is the remote procedure call protocol; JSON-RPC is RPC which is encoded with JSON. It is similar to XML-RPC. The difference is that JSON-RPC does not require a response for sending data to the server.

Odoo web controllers help in creating frontend modules. To know more about Odoo web controllers, Please refer the blog: Web Controllers in Odoo

Mainly we have two types of request parameters in Odoo web controllers, HTTP, and JSON requests.

 

HTTP Request:

One can define methods to get HTTP requests by passing ‘HTTP’ to the type of the argument of http.route(), then we will get HTTP parameters as the parameters of the method.


@http.route('/lead/case_mark_won', type='http', auth='user', methods=['GET'])
def crm_lead_case_mark_won(self, res_id, token):
comparison, record, redirect = MailController._check_token_and_record_or_redirect('crm.lead', int(res_id), token)
if comparison and record:
try:
record.action_set_won()
except Exception:
_logger.exception("Could not mark crm.lead as won")
return MailController._redirect_to_messaging()
return redirect 


JSON Request:

Define methods to get JSON requests by passing JSON to the type of the argument of http.route(). Odoo contacts these methods by using the JSON-RPC protocol. JSON methods must return JSON. They receive arguments as named parameters except if arguments are JSON-RPC parameters.

We need a .js file from which JSON data is sent to python code. For that create a file test.js with the following lines:


var ajax = require('web.ajax');
//To define ajax
var test_variable = document.getElementById("test_variable_id").value;
//To get value from the required field.
ajax.jsonRpc('/url, 'call', {
                       'test_variable' : test_variable,
                      });
//json Rpc Call
.then(function (data) {
});
//To receive data from python(non-mandatory)


Here, we pass a value test_value to the corresponding URL of the controller by calling ajax.jsonRpc. After performing operations in the method(from the controller) either one can return the result to the js file or execute from the controller itself.

Next is to add a method in the controller which could handle the Ajax request. This method would return the output data in a JSON format. For that add the following lines to the main.py file of the controller.


@http.route('/request', type='json', auth='user', methods=['POST'], website=True, csrf=False)
def submit(self,):
//perform operations here
   return {
               'result' : result
          }
//if no need to return result then return true


To receive data in the controller from JSON, the type should be JSON in the web controller.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message