accept标头 php,解决PHP中缺少“授权”请求标头的问题
我目前正在為一個(gè)uni項(xiàng)目開(kāi)發(fā)PHP REST API,該項(xiàng)目使用通過(guò)PhoneGap從移動(dòng)Web應(yīng)用程序傳遞的JSON Web令牌,或者在開(kāi)發(fā)過(guò)程中使用我的桌面.
使用ajax將令牌發(fā)送到我的服務(wù)器頁(yè)面“ friends / read.PHP”時(shí),服務(wù)器使用以下命令正確獲取了Authorization標(biāo)頭
$headers = getallheaders();
$authHeader = $headers['Authorization'];
但在多次成功運(yùn)行后停止這樣做.此后,將不再提取標(biāo)頭.
我的請(qǐng)求代碼如下:
$.ajax({
url: "http://localhost/chordstruck/api/friends/read.PHP",type: "GET",beforeSend: function (request) {
request.setRequestHeader('Authorization','Bearer ' + localStorage.getItem('jwt'));
},datatype: "json",success: function (response) {
console.log(response);
},error: function (jqXHR,textStatus,errorThrown) {
console.log(jqXHR);
}
});
奇怪的是,當(dāng)使用die(“ test”)過(guò)早地殺死PHP腳本,然后再次刪除die()時(shí),服務(wù)器將開(kāi)始為多個(gè)其他請(qǐng)求選擇Authorization標(biāo)頭.
Read.PHP:
error_reporting(E_ALL);
ini_set('display_errors','on');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET');
header('Access-Control-Allow-Headers: Origin,Content-Type,Authorization,X-Auth-Token');
$config = require_once '../config/core.PHP';
require_once '../config/jwt_helper.PHP';
// get database connection
include_once '../config/database.PHP';
// instantiate profile object
include_once '../objects/profile.PHP';
$headers = getallheaders();
$authHeader = $headers['Authorization'];
$token;
if ($authHeader) {
list($jwt) = sscanf((string)$authHeader,'Bearer %s');
if ($jwt) {
try {
$key = $config['jwt_key'];
$token = JWT::decode($jwt,$key,array('HS512'));
} catch (Exception $e) {
header('HTTP/1.0 401 Unauthorized');
exit();
}
} else {
header('HTTP/1.0 400 Bad Request');
exit();
}
} else {
header('HTTP/1.0 400 No Header Found');
exit();
}
echo "success";
?>
我在開(kāi)發(fā)此項(xiàng)目時(shí)遇到了CORS問(wèn)題,我在.htaccess文件中與上面的標(biāo)頭以及下面的標(biāo)頭對(duì)立了:
Header set Access-Control-Allow-Origin "*"
這可能相關(guān)嗎?任何幫助/想法將不勝感激!
總結(jié)
以上是生活随笔為你收集整理的accept标头 php,解决PHP中缺少“授权”请求标头的问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: android 知识点大全,Androi
- 下一篇: 从java代码获取类名_java代码获取