Search code examples
pythonjqueryajaxodoo

how to make a post request to odoo controller from outside odoo?


i am trying to get a form to ouside odoo so i built the form with website builder html_form_builder i generated the code of html by the module the form generated with action attribute

<form id="odoo_form" method="POST" action="http://localhost:8069/form/insert" enctype="multipart/form-data">
....
</form>

i extended the html with jquery code

$.ajax(my_form.attr('action'), {
            data: postData,
            processData: false,
            contentType: false,
            headers: {"Accept": "application/json" ,
            }, 
            success: function(data) {
                                    //alert('response data = ' + data);
                                    console.log('Success'); 
                                },
                                error: function (data) {
                                },
    type: 'POST'});

when you call jquery code the controller will be invoked

@http.route('/form/insert', type="http", auth="public", csrf=False)
    def my_insert(self, **kwargs):
        return self.process_form(kwargs)

the process_form will as expected

if form_error:
    return json.JSONEncoder().encode({'status': 'error', 'errors': return_errors})

i have seen the log the function invoked and make return / but i get this error in jquery

POST https://localhost:8069/form/insert net::ERR_FAILED

Solution

  • i have solved the problem by editing controller function

        @http.route('/form/insert', type="http", auth="public", csrf=False)
        def my_insert(self, **kwargs):
            body = self.process_form(kwargs)
            return request.make_response(body,{
                'Access-Control-Allow-Origin':  '*',
                'Content-Type': 'application/json',
                'Access-Control-Allow-Methods': 'GET',
                })