Search code examples
jquerycontrollerodoorpc

How to call the json data from web controller in Odoo


I am trying to call the method from a controller but failed to so. I always get error that createdb() has 2 arguments. Any idea how to get the value of db_name? Please give me some examples on how to do it. Your help is highly appreciated.

And this is what I did:

             <div class="form-group form-field o_website_form_required_custom">
               <label class="col-md-3 col-sm-4 control-label" 
                  for="partner_name">Company Name</label>
                    <div class="col-md-7 col-sm-8">
                        <input type="text"
                            class="form-control o_website_form_input"
                            name="partner_name" required=""
                            t-att-value="request.params.get('partner_name', '')" />
                    </div>
            </div>
    @http.route('/cystin/createdb', type='json', auth="public", methods=["POST"], website=True)
    def createdb(self, db_name):
        
        session = self._authenticate()
        if not session:
            return json.dumps(False)

        # create a new database
        headers = {'Content-Type': 'application/json'}
        
        create_db_url = "http://localhost:8090/saas_api/create_db"
        data = {"jsonrpc": 2.0, "params": { "name": db_name } } //how to get this data?

        _logger.debug("Creating database...")
        r = session.post(url=create_db_url, data=json.dumps(data), headers=headers)

        if r.ok:
            return json.dumps(True)
        else:
            return json.dumps(False)
    var session = require('web.session');
   

    $(function(){
        $("#trybutton").click(function()
        {
           var db_name = $('input').attr('name', 'partner_name').val();
          session.rpc('/cystin/createdb',
          {
            db_name: db_name,
          }).then(function() 
          {
            $("div.spanner").addClass("show");
            $("div.overlay").addClass("show")
          });
        });
    });


Solution

  • I solved it already. got confused in getting the correct attribute from xml.

    var db_name = $("input[name='partner_name']").val();