要使用 Vue 前端+ CodeIgniter 后端这种方式,第一个是跨域问题,从网上抄了一段写的挺详细的:
1 | // 1、对普通的GET/POST/PUT请求,请求头设置如下: |
但对于这里来说不用那么复杂,只需要设置 Access-Control-Allow-Origin 就行了,CI 的写法:
1 | $this->output->set_header("Access-Control-Allow-Origin: *");//"*"改为网站地址 |
Vue 中:
1 | let data= { |
CodeIgniter 中:
1 | public function test(){ |
试了试发现 Vue 正常,也能收到返回值,但 CI 却没办法收到 post 的数据,直接返回 null ,
后来发现 axios 里面写着:
1 | const params = new URLSearchParams(); |
把 Vue 部分改一下就行了:
1 | let params = new URLSearchParams(); |