我有一个网页,需要在 header 中发送 ID 和 token 才能加载页面。 PHP 登录脚本返回 id 和 token 。然后需要在页面的 header 中发送此 id 和 token 才能加载。提供检查来查找这些自定义 header ,如果未设置,则会重定向到登录页面。该页面需要从 JavaScript 函数加载。如果我使用 window.location,它会被重定向到登录页面,因为找不到标题。我如何实现这一目标?
这是我的代码:
$.ajax({
type: "POST",
url: "http://www.example.com/apis/login.php",
data: JSON.stringify(inputs),
dataType: "json",
success: function(data) {
if(data.status == "success") {
username = data.username;
uid = data.uid;
token = decodeURIComponent(data.token);
loadpanel(username, uid, token);
}
else{
alert(data.message);
}
},
error: function() {
alert("could not connect to server");
}
});
function loadpanel(username, uid, token) {
$.ajax({
async: true,
type: "GET",
url: "/haspanel.php",
headers: {
"U-Name" : username,
"U-Id" : uid,
"Auth-Token" : encodeURIComponent(token)
},
success: function(data) {
window.location='/haspanel.php';
},
error: function() {
alert("could not connect to server");
}
});
}
我知道,这行不通,但我不明白解决方法是什么。
最佳答案
你可以这样做吗? :
window.location = '/page.php?id='+ id +'&token='+ token +'';
其中 id
和 token
是包含 id 和变量参数所需值的变量。
关于javascript - 发送加载页面所需的自定义 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44583068/