php/loginManager.php
我有
require 'vendor/autoload.php';
require 'php/contentManager.php';
session_start();
use Parse\ParseClient;
ParseClient::initialize('XXXXX', 'XXXXX', 'XXXXX');
use Parse\ParseObject;
use Parse\ParseUser;
use Parse\ParseException;
use Parse\ParseQuery;
use Parse\ParseACL;
use Parse\ParseRole;
function checkLoggedIn(){
$currentUser = ParseUser::getCurrentUser();
if ($currentUser) {
echo 'logged in';
}
else {
echo 'NOT logged in';
}
}
settings.php
我有
include("php/loginManager.php");
checkLoggedIn();
当我在浏览器中加载settings.php
时,我会按预期“登录”。
我想通过ajax获得此结果。这就是我试过的:
var $target =$('#someElement');
var dataString ='someVar=someValue';
$.ajax({
type: "POST",
url: 'settings.php',
data: dataString,
success: function(payload) {
$target.html(payload);
},
error: function(xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
console.log(err.Message);
}
});
不幸的是,这会产生以下错误:
致命错误:在第333行的/Users/DOoDSoftware/Sites/project/vendor/parse/php-sdk/src/Parse/ParseUser.php中的非对象上调用成员函数get()
添加了var_dump的ParseUser.php
的第332-333行:
$storage = ParseClient::getStorage();
echo var_dump($storage); // this test echoes NULL
$userData = $storage->get("user"); // the line that generates the error
我将这个echo添加到了解析初始化函数中:
public static function initialize($app_id, $rest_key, $master_key, $enableCurlExceptions = true)
{
echo('parse initialized');
//....
}
我实际访问settings.php
时会打印“parse initialized”,但是当我进行ajax调用时则不会打印。
显然,当我进行ajax调用时,Parse没有被初始化。
我将呼叫转移到require 'php/contentManager.php';
,如下所示。
require 'vendor/autoload.php';
// require 'php/contentManager.php'; removed from here
session_start();
use Parse\ParseClient;
ParseClient::initialize('XXXXX', 'XXXXX', 'XXXXX');
use Parse\ParseObject;
use Parse\ParseUser;
use Parse\ParseException;
use Parse\ParseQuery;
use Parse\ParseACL;
use Parse\ParseRole;
require 'php/contentManager.php'; // added it back down here
它现在有效,但如果有人可以解释为什么这样可以解决问题,我真的很感激