话不多说直接上代码(直接用改改自己需要的逻辑即可)最后别忘记在__manifest__.py加权限
xml部分:
<?xml version="1.0" encoding="UTF-8"?> <odoo> <template id="register_page" > <head> <meta http-equiv="Access-Control-Allow-Origin" content="*" /> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.8/vue.min.js"></script> <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script> <script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.min.js"></script> <script type="text/javascript" src="/academy/static/src/js/test.js"></script> <script src="http://code.jquery.com/jquery-latest.js"></script> <style> .int{ height: 30px; text-align: left; width: 600px; } label{ width: 200px; margin-left: 20px; } .high{ color: red; } .msg{ font-size: 13px; } .onError{ color: red; } .onSuccess{ color: green; } </style> </head> <center><h3>用户信息页面</h3></center> <form name="myform"> <div class="int"> <label for="name">姓名:</label> <input type="text" id="name" name="name" class="required" /> </div> <div class="int"> <label for="mobile">电话:</label> <input type="number" id="mobile" name="mobile" class="required" /> </div> <div class="int"> <label for="street">住址:</label> <input type="text" id="street" name="street" /> </div> <div class="int"> <input type="button" value="1测试1" οnclick="sadly()" style="margin-left: 70px;" ></input> </div> </form> <script src="http://www.oschina.net/js/2012/jquery-1.7.1.min.js" type="text/javascript"></script> <script type="text/javascript"><![CDATA[ function jsonpCallback(res){//回调 jsonp 固定格式我也不知道为啥就这么写好用 //自己进行逻辑处理 console.log(res); alert(res.msg) } function sadly(){ var name = myform.name.value; var mobile = myform.mobile.value; var street = myform.street.value; //console.log(1); $.ajax({ type:"get", url:"http://localhost:8069/inserttest", dataType:"script",//是个坑不是jsonp很奇怪 data:{ name:name, phone:phone, address:address, }, jsonp:"callback", jsonpCallback:"success_jsonpCallback", success: function(data){ //alert("成功") //console.log(data); }, error: function(data){ alert("失败") console.log(data) } }); } ]]> </script> </template> </odoo>
controllers部分
# -*- coding: utf-8 -*- from odoo import http from odoo.http import request import time import json from odoo import api from odoo.tools import misc class Academy(http.Controller):
# 创建接口测试 @http.route('/inserttest', auth='public',methods=['POST','GET']) def inserttest(self,jsonp ='jsonpCallback',**args):#注意jsonp格式 name= args.get('name')#接受前端的参数 phone = args.get('phone') address =args.get('address') print(name) print(phone) print(address) Teachers = http.request.env['academy.teachers'] # [数据库] if name: product_count = Teachers.sudo().search_count([('name', '=', name)])#查询有没有此人 # print(product_count) if (product_count)>=1: res = {'msg': '用户已存在!', 'msg_code': 2001} else: Teachers.sudo().create({'name': name,'phone':phone,'address':address} ) res = {'msg': '注册成功!', 'msg_code': 0} else: res = {'msg': '必填字段未填,请检查接口文档!', 'msg_code': 1001} return_value = (jsonp +"("+ json.dumps(res,ensure_ascii=False)+")")#json固定格式 return return_value