当前位置:网站首页>Understand │ what is cross domain? How to solve cross domain problems?
Understand │ what is cross domain? How to solve cross domain problems?
2022-07-27 18:07:00 【InfoQ】
What is cross-domain
- Domain :It means that the browser cannot execute scripts of other websites
- Cross domain :It's run by the browserThe same-origin policyCaused by the , It 's the browser
JavaScriptSecurity restrictions imposed , The so-called homology ( I mean in the same domain ) Two pages have the same protocolprotocol, hosthostAnd port numberportWill causeCross domain

Cross domain scenario
- What are the cross domain scenarios of the scenario , Please refer to the table below

There are four ways to solve cross domain problems
- nginx Reverse proxy of
- Use
nginxReverse proxy implementation across domains , It's the easiest way to cross domains
- It just needs to be modified
nginxIs configured to solve cross-domain problems , Support for all browsers , Supportsession, You don't need to change any code , It does not affect server performance
// nginx To configure
server {
listen 81;
server_name www.domain1.com;
location / {
proxy_pass http://www.domain2.com:8080; # Reverse proxy
proxy_cookie_domain www.domain2.com www.domain1.com; # modify cookie In the domain name
index index.html index.htm;
# When used webpack-dev-server Such as middleware proxy interface access nignx when , No browser is involved at this time , So there is no homologous restriction , The following cross-domain configuration is not enabled
add_header Access-Control-Allow-Origin http://www.domain1.com; # The current end is cross-domain only cookie when , for *
add_header Access-Control-Allow-Credentials true;
}
}- jsonp request
jsonpIt is a common method of cross source communication between server and client . The biggest characteristic is simple and applicable , Compatibility is good.Compatible with lower versions IE, The disadvantage is that it only supportsgetrequest , I won't support itpostrequest
- The principle of web pages is to add a
<script>Elements , Request... From the serverjsondata , After the server receives the request , Put the data in the parameter position of a callback function with a specified name and pass it back
//jquery Realization
<script>
$.getJSON('http://autofelix.com/api.php&callback=?', function(res) {
// Process the data obtained
console.log(res)
});
</script>- Back end language agent
- You can transfer it through a language without cross domain restrictions , Request resources through the back-end language , And then it returns the data
- such as
http://www.autofelix.cnNeed to callhttp://api.autofelix.cn/userinfoTo get user data , Because the subdomain name is different , There will be cross domain restrictions
- You can ask first
http://www.autofelix.cnUnder thephpfile , such ashttp://www.autofelix.cn/api.php, And then through thatphpThe file returns data
<?php
// api.php Code in file
public function getCurl($url, $timeout = 5)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$result = getCurl('http://api.autofelix.cn/userinfo');
return $result;- Back end language settings
- It is mainly through the back-end language to actively set cross domain requests , Here we use
phpAs a case study
<?php
// Allow access to all domain names
header('Access-Control-Allow-Origin: *');
// Allow a single domain name to access
header('Access-Control-Allow-Origin: https://autofelix.com');
// Allow multiple custom domain names to access
static public $originarr = [
'https://autofelix.com',
'https://baidu.com',
'https://csdn.net',
];
// Get the current cross domain domain name
$origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';
if (in_array($origin, self::$originarr)) {
// allow $originarr In the array Domain name cross domain access
header('Access-Control-Allow-Origin:' . $origin);
// Response type
header('Access-Control-Allow-Methods:POST,GET');
// belt cookie Cross domain access to
header('Access-Control-Allow-Credentials: true');
// Response header Settings
header('Access-Control-Allow-Headers:x-requested-with,Content-Type,X-CSRF-Token');
}边栏推荐
- 细数国产接口协作平台的六把武器!
- Branch loop statement of C language
- Helm install kubevela complete makefile script content
- Oracle 11g database installation tutorial
- Configuration and basic use of vim
- Cow! His secret is to reproduce the paper in 2 hours——
- Database hyperphone (4)
- Mysql database defines cursor in trigger
- 泰山OFFICE技术讲座:WORD奇怪的段落边框
- How to develop an online Excel spreadsheet system (Part 1)
猜你喜欢

【cf】#681 A. Kids Seating (Div. 2, based on VK Cup 2019-2020 - Final)

灵魂一问:为什么ES比MySQL更适合复杂条件搜索?

Convolutional neural network -- Translation of yolov2 (yolo9000) papers

Array of C language

知物由学 | 再造巴别塔,我们如何进行NLP跨语言知识迁移?

Wechat applet to make calls

How to resolve the public domain name to the intranet IP server -- quickly resolve the domain name and map the Internet access

Cow! His secret is to reproduce the paper in 2 hours——

Interviewer: what is scaffolding? Why do you need scaffolding? What are the commonly used scaffolds?

How to develop an online Excel spreadsheet system (Part 1)
随机推荐
likeshop外卖点餐系统「100%开源无加密」
Does PostgreSQL 14 support winserver2022?
Zhengzhou University database course resource description
美团到餐“祖传数仓”标准化治理笔记
2022 safety officer-c certificate special operation certificate examination question bank and answers
微信小程序 云函数批量删除多条数据 Error: errCode: -502005 database collection not exists
Convolutional neural network -- SSD thesis translation
TCP connection status identification (syn, fin, ACK, PSH, RST, urg)
[introduction to database system (Wang Shan)] Chapter 5 - database integrity
Notes on standardized management of "ancestral warehouse" of meituan meal
What are the safety risks of small games?
x-sheet 开发教程:初始化配置自定义布局
机器学习之评价指标(二)——分类评价指标
Convolutional neural network -- Introduction to FPN (feature pyramid networks)
Functions of C language
知物由学 | 再造巴别塔,我们如何进行NLP跨语言知识迁移?
Summer Challenge [FFH] real time chat room websocket practice
【Codeforces】 B. Make it Divisible by 25
Yyds dry inventory interview must brush top101: specified interval reversal in the linked list
知物由学 | APP大瘦身,新一代AAB框架下的安全加固之道