[英]handling nested file include in php and returning value to javascript function
在login.php
按钮上单击“我执行redirect.php
。 此redirect.php执行Twitter身份验证。 为此,它调用其他文件,而后者又调用index.php
。
index.php
给出结果name
和id
,我想在index.php
检索它。
源代码: Git源代码
当前发生了什么,login.php在$.get()
login.php
响应之前执行$.get()
并警告indefined value
的login.php
<body>
<input type="button" value="Run somePHPfile.php" id = "b1" />
<script>
$('#b1').click(function () {
window.location.href = 'redirect.php';
//This function needs improvement i think, is this correct place for this to get json value from index.php??
$.get('index.php', function(data) { //If I put this out side click then it gives undefined value for name and id before redirect.php gets executed
// data.id is the id
var id= data.id;
var name = data.name;
alert(name);
});
});
</script>
</body>
redirect.php
<?php
/* Start session and load library. */
session_start();
require_once('twitteroauth/twitteroauth.php');
require_once('config.php');
... //some other processing
?>
的index.php
<?php
session_start();
require_once('twitteroauth/twitteroauth.php');
require_once('config.php');
if (empty($_SESSION['access_token']) || empty($_SESSION['access_token']['oauth_token']) || empty($_SESSION['access_token']['oauth_token_secret'])) {
header('Location: ./clearsessions.php');
}
$access_token = $_SESSION['access_token'];
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
$content = $connection->get('account/verify_credentials');
$twitteruser = $content->{'screen_name'};
$notweets = 5;
$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets);
$name = $content->{'name'};
$id = $content->{'id'};
echo json_encode((object) array('id' => $id, 'name' => $name)); //to retreive in login.php
?>
UPDATE
$ .get('index.php',function(data){var user_id = data.id; var name = data.name; alert(name); // alert(name); window.location.href ='按钮。 PHP的“;
}, “JSON”);
由于您的php文件正在输出一个json字符串,因此您的get调用需要知道它正在检索这样的字符串
$.get('index.php', function(data) {
var id= data.id;
var name = data.name;
alert(name);
},"json");
这将让jquery知道它需要将传入的数据解析为json字符串并将其解析为一个对象,否则您将不得不自己解析
$.get('index.php', function(data) {
data = JSON.parse(data);
var id= data.id;
var name = data.name;
alert(name);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.