繁体   English   中英

jQuery 提取令牌并将其传递给 php 位置标头

[英]jQuery extract token and pass it to php location header

我想使用 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();
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM