[英]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.