[英]getJSON and OAuth Token
I'm working on a script that uses jQuery (getJSON) to pullback values from a PHP page. 我正在使用jQuery(getJSON)从PHP页面提取值的脚本。 The PHP page correctly displays the encoded object with the correct keys and values, with the values being retrieved from the $_SESSION
variable. PHP页面正确显示具有正确键和值的编码对象,并从$_SESSION
变量中检索值。 However when the javascript attempts to access this page, the object is retrieved, but only the keys are intact with the values being replaced with null
. 但是,当javascript尝试访问此页面时,将检索该对象,但只有键完整无缺,而值将替换为null
。 The values that are returning null
are retrieved from the $_SESSION
, whereas any values that are manually added are correctly returning. 从$_SESSION
检索返回null
的值,而正确添加任何手动添加的值。 Is this something that can be avoided/fixed? 这是可以避免/解决的问题吗? Would it make more sense to store the access tokens in cookies rather than sessions? 将访问令牌存储在cookie中而不是会话中是否更有意义?
Code examples: 代码示例:
<?php
session_start();
$token = $_SESSION['token'] -> access_token;
$information = [
"media_token" => $token,
"test" => "testing"
];
print json_encode($information); //when page is directly accessed,
//returns correct information
?>
Javascript: Javascript:
$.getJSON('oauth.php', function (data){
console.log(data); // media_token: null
// test: testing
});
Try this code it will help you 试试这个代码将对您有帮助
<html>
<head>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script>
$(document).ready(function() {
$.getJSON('oauth.php', function (data){
console.log(data); // media_token: null
// test: testing
});
});
</script>
</head>
<body>
<div class="title">
<h2>PRESS</h2>
<p>hello</p>
</div>
</body>
</html>
oauth.php file oauth.php文件
session_start();
$_SESSION['token']="Hello1212";
$token = $_SESSION['token'];// -> access_token;
$information = array(
"media_token" => $token,
"test" => "testing"
);
print json_encode($information); //when page is directly accessed,
//returns correct information
Turns out the issue was that I was accessing the page that contained the JS using a non-www page, whereas adding www fixed the problem. 原来的问题是我正在使用非www页面访问包含JS的页面,而添加www解决了该问题。 Not sure why but clearly this changed if the page could access the $_SESSION or not. 不知道为什么,但是很明显,如果页面可以访问$ _SESSION,则此更改。 If anyone could explain why this is the case that'd be great, cheers. 如果有人能解释为什么会这样,那会很高兴。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.