Php 二次跨域
我想从网站前端的跨域访问数据。 我有三个网站:a.com、b.com和c.com。网站a.com是前端。网站b.com是后端(API)。c.com是CAS服务器 我可以直接在浏览器上访问。它成功跳转到CAS页面进行登录。然后在登录后,API返回数据 我想让a.com获取b.com的数据。我的代码是: 我的前端html:Php 二次跨域,php,html,cross-domain,cas,Php,Html,Cross Domain,Cas,我想从网站前端的跨域访问数据。 我有三个网站:a.com、b.com和c.com。网站a.com是前端。网站b.com是后端(API)。c.com是CAS服务器 我可以直接在浏览器上访问。它成功跳转到CAS页面进行登录。然后在登录后,API返回数据 我想让a.com获取b.com的数据。我的代码是: 我的前端html: <!DOCTYPE html> <html lang="en"> <head> <meta ch
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="Access-Control-Allow-Origin" content="*">
<title>Title</title>
</head>
<body>
<h3 id="article_title"></h3>
<p id="article_text"></p>
</body>
<script type="application/javascript">
var xmlHttpReq = null;
if (window.ActiveXObject) {
xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
} else if (window.XMLHttpRequest) {
xmlHttpReq = new XMLHttpRequest();
}
if (xmlHttpReq !== null) {
xmlHttpReq.open("get", "http://b.com/example_test.php");
xmlHttpReq.timeout = 10000;
xmlHttpReq.send("");
xmlHttpReq.onreadystatechange = doResult;
}
function doResult() {
if (xmlHttpReq.readyState === 4) {
console.log(xmlHttpReq.status);
if (xmlHttpReq.status === 200) {
var data = xmlHttpReq.responseText;
var json_data = JSON.parse(data);
/**
do somethin.
*/
}
}
}
</script>
</html>
标题
var xmlHttpReq=null;
if(window.ActiveXObject){
xmlHttpReq=新的ActiveXObject(“Microsoft.XMLHTTP”);
}else if(window.XMLHttpRequest){
XMLHttpRequest=新的XMLHttpRequest();
}
if(xmlHttpReq!==null){
xmlHttpReq.open(“get”http://b.com/example_test.php");
xmlHttpReq.timeout=10000;
xmlHttpReq.send(“”);
xmlHttpReq.onreadystatechange=doResult;
}
函数doResult(){
if(xmlHttpReq.readyState==4){
日志(xmlHttpReq.status);
if(xmlHttpReq.status==200){
var数据=xmlHttpReq.responseText;
var json_data=json.parse(数据);
/**
做点什么。
*/
}
}
}
我的后端api示例_test.php:
<?php
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:GET,POST,PUT,DELETE,PATCH,HEAD,OPTIONS');
header('Access-Control-Allow-Headers:Origin,Content-Type,Authorization,X-auth-Token');
require_once './MyCAS.php';
$resp = array(
'stats' => -1,
'msg' => 'Empty!'
);
if (array_key_exists('username', $_COOKIE)) {
/**
do something
**/
} else {
phpCAS::client(CAS_VERSION_2_0, "mycase.server.com", 443, "/cas", false);
phpCAS::setNoCasServerValidation();
phpCAS::forceAuthentication();
if (phpCAS::isAuthenticated()) {
$username = phpCAS::getUser();
if ($username !== null) {
$token = md5($username.date('s', time()).date('i', time()));
setcookie('username', $username, time() + 1*86400, '/');
setcookie('token', $token, time() + 1*86400, '/');
/**
do something
**/
}
}
}
?>
但当我在浏览器上访问a.com时,它不会跳转到CAS页面。我不明白这个问题。有人能帮我解决这个问题吗?您不能通过ajax重定向调用方的请求。用JavaScript实现它的一种方法 PHP示例:
<?php
echo 'redirect';
您可以检查浏览器网络中的ajax加载情况以及它发送的响应是什么。@Rohit.007我访问a.com,浏览器收到的cas服务器的页面内容和代码是200,但它不跳转。跳转是指它不会重定向到cas服务器的页面上?@Rohit.007是的,浏览器控制台可以找到页面内容,预览是页面,但不跳转。很奇怪。@Rohit.007你可以试试我的代码。您可以将cas服务器代码替换为标头('Location:your c.com');
$.ajax({
type: 'POST',
async: false,
url: '/users',
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify(payload),
success: function(data, textStatus, jqXHR){
if(data == 'redirect'){
console.log(jqXHR.status);
window.location.href= "/thankyou.html";
}
}
});