同源策略:同源策略是一種安全機(jī)制。瀏覽器禁止通過腳本發(fā)起跨域請求,如XMLhttpRequest,多個頁面的協(xié)議、域名和端口完全相同,則認(rèn)為他們遵循了同源策略
CSRF:跨站請求偽造。攻擊者盜用用戶身份,以用戶的名義發(fā)送惡意請求
CORS:跨域資源共享。用額外的 HTTP 頭來告訴瀏覽器 讓運(yùn)行在一個 origin (domain) 上的Web應(yīng)用被準(zhǔn)許訪問來自不同源服務(wù)器上的指定的資源
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
console.log(xhr.responseText);
}
};
xhr.open("GET", "http://test2.com/handle.php", true);
xhr.send(null);
</script>
</body>
</html>
<?php
header("Access-Control-Allow-Origin: http://test1.com");
echo 'hello';