tp5.1 与vue ajax请求跨域的问题
header('Access-Control-Allow-Origin: *'); header("Access-Control-Allow-Headers:Origin, X-Requested-With, Content-Type, Accept"); header('Access-Control-Allow-Methods: POST,GET');
在router/router.php头上写上上面的header头就可以正常调用了。
解决办法:
给请求头添加 X-Requested-With 这个参数,参数值为 XMLHttpRequest
// 以 axios 为例 this.$axios.get('http://yourdomain.com/api/getMenus', { headers: {'X-Requested-With': 'XMLHttpRequest'} }).then((res)=>{ // console.log(res.data); })
- 上一篇 »php 服务端允许跨域访问
- 下一篇 »php服务器端解决跨域问题