thinkphp ajax 跨域请求 Access-Control-Allow-Origin 完美解决
来源:互联网 发布:python hmmlearn库 编辑:程序博客网 时间:2024/06/05 22:33
ajax跨域请求出问题,一般有以下几种情况:
1、直接打开本地文件夹的 .html等文件来ajax请求服务器数据,浏览器网址显示类似如下,以“file”开头:
file:///Users/username/wwwroot/test/Application/Api/View/Index/a.html
//解释:这种方法是不被允许的,不能用file类型来请求数据
把a.html放到服务器上,用网址的形式打开,如下:
http://www.test.com/Application/Api/View/Index/a.html
2、用网址打开了还是无效,比如客户端是本地搭建的环境,服务器端在阿里云或者别的网络服务器,这种情况有两种解决办法:
a、用jsonp,不具体说了,这个方法治标不治本,需要注意的是,使用jsonp的时候,只有get 类型可用,即使设置了 post类型,还是使用的 get
b、在服务器端的代码中加入如下代码:
// 指定允许其他域名访问 header('Access-Control-Allow-Origin:*'); // 响应类型 header('Access-Control-Allow-Methods:*'); // 响应头设置 header('Access-Control-Allow-Headers:x-requested-with,content-type');
//解释:比如在thinkphp中,网络接口位于目录 Application/Api/Controller/BaseController.class.php ,该控制器里有个getInfo() 方法,客户端通过 xxx.com/Api/Base/getInfo 接口ajax读取服务器返回的数据,我们就把 上述三个header()放到这个控制器里,如果所示:
阅读全文
0 0
- thinkphp ajax 跨域请求 Access-Control-Allow-Origin 完美解决
- Access-Control-Allow-Origin解决Ajax跨域问题
- 解决Ajax跨域问题:Origin xx is not allowed by Access-Control-Allow-Origin.
- 解决Ajax跨域问题:Origin xx is not allowed by Access-Control-Allow-Origin
- ajax请求node.js接口时出现跨域问题Access-Control-Allow-Origin
- ajax跨域请求:No 'Access-Control-Allow-Origin' header is present on the requested resource
- Ajax访问Servlet的跨域请求问题—Access-Control-Allow-Origin
- xmlhttprequest cannot load No 'Access-Control-Allow-Origin' AJAX跨域请求解决方法
- No 'Access-Control-Allow-Origin' header is present on the requested resource.ajax跨域请求
- Ajax跨域请求的两种方式(jsonp和Access-Control-Allow-Origin)
- Access-Control-Allow-Origin跨域请求以及隐患
- access-Control-Allow-Origin跨域请求安全隐患
- Access-Control-Allow-Origin 站点跨域请求的问题
- Access-Control-Allow-Origin跨域请求php+js
- Nginx配置跨域请求 Access-Control-Allow-Origin *
- ajax Access-Control-Allow-Origin跨域问题
- ajax 设置Access-Control-Allow-Origin实现跨域访问
- Access-Control-Allow-Origin:ajax 跨域访问
- PHP 安装
- Windows上优秀的命令行(Terminal Emulator)工具推荐
- leetcode--Surrounded Regions
- Yii2.0前后台登录分离
- leetcode--Palindrome Partitioning
- thinkphp ajax 跨域请求 Access-Control-Allow-Origin 完美解决
- leetcode--Clone Graph
- 剑指offer面试题6 重建二叉树(java实现)
- JAVA企业面试题精选 Web基础 31-40
- python contextlib 上下文管理
- 14.文件系统:高大上的东西——import关键字/模块
- leetcode--Single Number
- IEnumerable和IEnumerator 详解
- c++中赋值操作符的重载