PHP-Ajax Cross-Domain Problem Best Solution
Preface Ajax cross-domain problems are very common in development. For example, the domain of the webpage is client.abc.com, while the domain of the request is server.abc.com. If you access it directly with ajax, you will get the following error: XMLHttpRequest cannot load http://server.abc.com/server.php. No 'Access-Control-Allow-Origin' header is present
目录7 个章节
Preface
Ajax cross-domain problems are very common in development. For example, the domain of the webpage is client.abc.com, while the domain of the request is server.abc.com. If you access it directly with ajax, you will get the following error:
XMLHttpRequest cannot load http://server.abc.com/server.php. No 'Access-Control-Allow-Origin' header is present on the requested resource.Origin 'http://client.abc.com' is therefore not allowed access.
The Simplest Solution
header('content-type:application/json;charset=utf8');
// 指定允许其他域名访问
header('Access-Control-Allow-Origin:*');
// 响应类型
header('Access-Control-Allow-Methods:POST');
// 响应头设置
header('Access-Control-Allow-Headers:x-requested-with,content-type');
1. Allow Access from a Single Domain
To allow a specific domain client.abc.com to make cross-domain requests, you only need to add the following code at the top of the server.abc.com/a.php file:
header('Access-Control-Allow-Origin:http://client.abc.com');
2. Allow Access from Multiple Domains
To allow multiple domains, add the following code at the top of the server.php file:
$origin = isset($_SERVER['HTTP_ORIGIN'])? $_SERVER['HTTP_ORIGIN'] : '';
//$_SERVER['HTTP_ORIGN']获得请求访问的域名
$allow_origin = array(
'http://client.abc.com',
'http://client2.abc.com'
);
if(in_array($origin, $allow_origin)){
header('Access-Control-Allow-Origin:'.$origin);
}
3. Allow Access from All Domains
To allow all domains to access, you only need to add the following code at the top of the php file:
header('Access-Control-Allow-Origin:*');
About
To learn more interesting tricks, please follow my WeChat official account: DealiAxy Every article is collected on my blog: blog.deali.cn
_EOF_
PHP-Ajax Cross-Domain Problem Best Solution
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
2023年的PHP项目部署笔记。什么?还有人用PHP?
PHP?2023年还在用?Docker部署心累,不如直接装php-fpm和nginx省心。权限问题?chown一下搞定,composer安装依赖完美运行。
2023-12-12 · PHP · 约 1 分钟
PHP7-0-新特性
变量类型 PHP7版本函数的参数和返回值增加了类型限定。 为什么PHP要加入类型呢?实际上此项特性是为了PHP7.1版本的JIT特性做准备,增加类型后PHP Jit可以准确判断变量类型,生成最佳的机器指令。 (针对密集运算的优化) 使用示例 function test(int $a, string $b, array $c) : int { //code } 错误异常 在过去,PHP程序出
2021-12-18 · PHP · 约 1 分钟
PHP7-0-+-Swoole-环境配置
前言 Swoole是一个面向生产环境的 PHP 异步网络通信引擎。 使 PHP 开发人员可以编写高性能的异步并发 TCP、UDP、Unix Socket、HTTP,WebSocket 服务。Swoole 可以广泛应用于互联网、移动通信、企业软件、云计算、网络游戏、物联网(IOT)、车联网、智能家居等领域。 使用 PHP + Swoole 作为网络通信框架,可以使企业 IT 研发团队的效率大大提升,
2021-12-18 · PHP · 约 1 分钟
Asp-Net-Core学习笔记:3.使用SignalR实时通信框架开发聊天室
SignalR牛刀小试 在MVP杨老师的博客里看到这么个东西,我还以为是NetCore3才推出的新玩意,原来是已经有很多年的历史了,那看来还是比较成熟的一个技术了。 简介 SignalR是一个.NET Core/.NET Framework的开源实时框架,SignalR的可使用Web Socket, Server Sent Events 和 Long Polling作为底层传输方式。 Signal
2022-02-16 · Asp-Net-Core学习笔记 · 约 1 分钟
讨论
共 0 条加载评论中…
