javascript - jQuery 提取令牌并将其传递给 php 位置标头
问题描述
我想使用 jQuery 从 API 中提取令牌,然后将其传递给 URL 中的 php 位置标头参数。
例子:
<script>
$.getJSON('https://example.com/APIENDPOINT', function(data) {
alert(data.access_token)
});
</script>
<?php
header('Location: https://example.com/auth/?access_token=${data.access_token}');
?>
我不确定我做错了什么,如果有人可以给我任何提示,我会很高兴。
解决方案
可能是这样的:
$.getJSON('https://example.com/APIENDPOINT', function(data) {
window.location.href = 'https://example.com/auth/?access_token=' + data.access_token;
});
如果需要 301 重定向,则仅在服务器端运行(php)
<?php
$json = file_get_contents('https://example.com/APIENDPOINT');
$obj = json_decode($json);
$token = $obj->access_token;
$url = 'https://example.com/auth/?access_token=' . $token ;
header("Location: ".$url, true, 301);
exit();
?>
推荐阅读
- javascript - 在 Contao 版本更新后,用于网站平滑滚动的 Javascript 代码不起作用
- typescript - 在 Ionic 3 中承诺后不返回任何数据
- html - 覆盖表格特定单元格的单元格填充
- typescript - 使用 outputPath 中的函数的 Webpack 文件加载器对目录进行 EISDIR 非法操作
- java - 我们如何在 kotlin 中访问 java 中的 .class 文件?
- javascript - 如何通过 http 从 mongoDB 发送大量文档?
- java - application.properties 与 applicationContext.xml
- ios - UICollectionView flowLayout的原理是什么
- android - 单击图标时,BottomNavigationBarItem 打开相应的页面
- php - 在我的 laravel 项目中,assertStatus(200) 的 PHPUnit 测试失败