Javascript 正在AJAX POST上丢失$\u会话变量。。。以前在工作吗
在说其他话之前,是的,我确实要开始会话;在每个相关页面的顶部。这不是问题所在 我正在使用谷歌分析API,有一个页面可以查询分析数据。我使用多个下拉菜单允许用户选择他们想要查询的帐户 为了填充这些下拉菜单,我使用AJAX调用API调用来填充每个下拉菜单,具体取决于上一个下拉菜单的值是think country->state->city 无论如何,在我不得不更新API库之前,一切都很正常。现在,我在发出AJAX POST请求的脚本和处理AJAX的脚本之间丢失了$\会话变量 这是我的密码: php这三个下拉菜单,只需注意它们是如何实例化的Javascript 正在AJAX POST上丢失$\u会话变量。。。以前在工作吗,javascript,php,jquery,ajax,session,Javascript,Php,Jquery,Ajax,Session,在说其他话之前,是的,我确实要开始会话;在每个相关页面的顶部。这不是问题所在 我正在使用谷歌分析API,有一个页面可以查询分析数据。我使用多个下拉菜单允许用户选择他们想要查询的帐户 为了填充这些下拉菜单,我使用AJAX调用API调用来填充每个下拉菜单,具体取决于上一个下拉菜单的值是think country->state->city 无论如何,在我不得不更新API库之前,一切都很正常。现在,我在发出AJAX POST请求的脚本和处理AJAX的脚本之间丢失了$\会话变量 这是我的密码: php这三
if ($client->getAccessToken()) { //if the client has a valid access token
try {
//lists the client's available analytic accounts
$accounts = $analytics->management_accounts->listManagementAccounts();
$accsAvailable = $accounts->getItems(); ?>
<p>Available Accounts:</p> <?php
if (!isset($_REQUEST['compare'])) { ?>
<form action="" name="formSubmit" onsubmit="return validateForm()" method="post"> <!--START FORM--> <?php
} else { ?>
<form action="" name="formSubmit" onsubmit="return validateComparison()" method="post"> <!--START FORM--> <?php
} ?>
<select name="dropAccounts" class="dropAccounts" id="dropAccounts"> <!--drop-down for avaiable accounts--> <?php
//if there is at least one account available
if (count($accsAvailable) > 0) { //create a drop down of available accounts ?>
<option value="0">---Select an account---</option> <?php //default option
foreach ($accsAvailable as $account) {
//populate from API
echo '<option value=' . $account->getId(). '>' . $account->getName() . '</option>';
}
} else { ?>
<option value="0">---No accounts available---</option> <?php //else no accounts exist
} ?>
</select> <!--END drop-down for avaiable accounts-->
</div>
<!-- /.col-md-4 -->
<div class="col-md-4">
<p>Available Webproperties:</p> <!--drop-down for available webproperties-->
<select name="dropProperties" class="dropProperties" id="dropProperties">
<option selected="selected" value="0">---Select a webproperty---</option> <!--default option-->
</select> <!--END drop-down for available webproperties-->
</div>
<div class="col-md-4">
<p>Available Profiles: </p> <!--drop-down for available profiles-->
<select name="dropProfiles" class="dropProfiles" id="dropProfiles">
<option selected="selected" value="0">---Select a profile---</option> <!--default option-->
</select> <!--END drop-down for available profiles-->
</div>
</div>
<!-- /.row -->
在第一个下拉菜单上进行选择时,将使用以下函数发出AJAX请求:
Javascript AJAX:
<script type="text/javascript">
$(document).ready(function() {
//populates the properties drop-down from API call
function populateProperties(accountID) {
$.ajax
({
type: "POST",
url: "/scripts/propertyID.php",
data: {
'accountID' : accountID //sends account ID for processing
},
cache: false,
success: function(html) {
$("#dropProperties").html(html); //refresh the properties drop-down
populateProfiles($("#dropProperties > option:selected").val()); // Populate profiles after properties load
}
});
}
//populates the profiles drop-down from API call
function populateProfiles(propertyID) {
$.ajax
({
type: "POST",
url: "/scripts/profileID.php",
data: {
'propertyID' : propertyID //sends property ID for processing
},
cache: false,
success: function(html) {
$("#dropProfiles").html(html); //refresh the profiles drop-down
}
});
}
//onchange event - repopulates properties and profiles after changing account
$("#dropAccounts").change(function() {
var accountID = $("#dropAccounts > option:selected").val(); //gets the account ID from drop-down value
populateProperties(accountID); //repopulates properties drop-down
});
//onchange event - repopulates profiles after changing properties
$("#dropProperties").change(function(){
var propertyID = $("#dropProperties > option:selected").val(); //gets the profile ID from drop-down value
populateProfiles(propertyID); //repopulates the profiles drop-down
});
});
</script>
这是一个未接收任何$\u会话变量的脚本:
propertyID.php
<?php
session_start();
//required Google Analytics libraries
set_include_path("../../lib/google-api-php-client-1.0.4-beta/src");
require_once 'Google/Client.php';
require_once 'Google/Service/Analytics.php';
if($_POST['accountID'])
{
$account = $_POST['accountID'];
$_SESSION['account_id'] = $accountID;
$client = $_SESSION['client'];
if ($client != null) {
$analytics = new Google_Service_Analytics($client);
if ($accountID != "0") {
$webProperty = $analytics->management_webproperties->listManagementWebproperties($accountID);
$webItem = $webProperty->getItems();
foreach ($webItem as $item) {
echo '<option value=' . $item->getId() . '>' . $item->getName() . '</option>';
}
}
}
} else {
echo '<option value="0">---Select a webproperty---</option>';
}
?>
我从propertyID.php中的$_SESSION['client']接收到null,是的,我将它保存到了一个会话变量中。事实上,当我刷新index.php时,允许我访问API的cookies也被清除。某个地方正在清理会话,但我不确定如何处理此问题
如有任何建议,将不胜感激 解决了我的问题 不管出于什么原因,我升级到的库给了我一些问题,并删除了我的会话。这毫无意义,但换一个版本解决了问题
感谢所有努力阅读我的问题的人。有一个解决方法,您可以调用
session_start($session-id);
并随时开始一个会话
您可以通过读取$\u cookies变量中的cookies来获取会话id,就是这样。会话cookie正在消失,或者每次会话id的值发生变化时,您都会获得不同的会话。发生这种情况的原因是什么?奇怪的是,考虑到我所做的唯一一件事就是更新库,这种情况会发生。编辑:我刚刚检查了两个脚本之间的网络日志,两个脚本上的PHPSESSID是相同的。您是否验证了ajax调用以查看它是否正在传递会话id cookie?。另一个原因可能是服务器上的不同域或不同文件夹不共享相同的cookies。@user3621911如何做到这一点?以前,当它工作时,我不必这样做使用浏览器的开发人员工具查看实际请求。