SilverStripe-JavaScript变量未通过AJAX成功发送到Page.php
我试图在模板中向SilverStripe-JavaScript变量未通过AJAX成功发送到Page.php,javascript,php,ajax,silverstripe,Javascript,Php,Ajax,Silverstripe,我试图在模板中向Page.php中名为“getMemberships”的函数发送一个JavaScript变量,但在使其工作时遇到了很多麻烦 我知道JavaScript函数ssCallBack确实可以工作,并从SharpSpring获取用户信息。我知道,如果您在此处硬编码电子邮件,则getMemberships功能会起作用: $lists = $this->sharpSpringService->makeCall('getListMemberships', [ 'emailAdd
Page.php
中名为“getMemberships”的函数发送一个JavaScript变量,但在使其工作时遇到了很多麻烦
我知道JavaScript函数ssCallBack
确实可以工作,并从SharpSpring获取用户信息。我知道,如果您在此处硬编码电子邮件,则getMemberships
功能会起作用:
$lists = $this->sharpSpringService->makeCall('getListMemberships', [
'emailAddress' => 'youremail@site.com',
]);
我知道您可以使用AJAX向PHP提交JavaScript数据,我已经设置了AJAX,并尝试使用各种设置(尝试数据类型:“json”,数据类型:“jsonP”,数据类型:“html”),但在getMemberships
中,$this->getRequest()->postVars('contactemail')
始终是一个空数组($contactemail={array}[0]
)。我不明白为什么会这样
在Page.ss文件中:
function ssCallBack(resp) {
if (typeof resp.contact != 'undefined') {
//this contact exists in SharpSpring
console.log(resp.contact['Email']);
var isSubscribed = false;
var contactEmail = resp.contact['Email'];
console.log(contactEmail);
$.ajax({
type: "POST",
url: "/home/getMemberships",
data: {
contactemail:contactEmail
}
}).done(function (response) {
for (var i = 0; i < response.length; i++) {
console.log(response[i].status);
var status = response[i].status;
var list = response[i].list;
if (status == "true") {
if(list =="PositionistView"){
isSubscribedToPV = true;
}
}
}
});
}
您需要
postVar('contactemail')
postVars()
返回所有post变量。使用postVar()也不起作用。它只返回一个空值,这是不正确的。我解决了这个问题。我正在测试的电子邮件实际上并不是我在getMemberships代码中引用的订阅列表的一部分,长话短说,这把一切都搞糟了。
public function getMemberships(){
$pvListID = [REDACTED];
$listName = "[REDACTED]";
$inPVList = false;
$contactEmail = $this->getRequest()->postVars('contactemail');
$converted_result ="";
$pvSubscription = null;
$return = [];
$sharpSpringService = null;
$this->sharpSpringService = new SharpSpringService("[REDACTED", "[REDACTED");
if($contactEmail != null && $contactEmail !=""){
$lists = $this->sharpSpringService->makeCall('getListMemberships', [
'emailAddress' => $contactEmail,
]);
if (count($lists) > 0) {
$listArray = json_decode(json_encode($lists), true);
foreach($listArray as $list){
if($list = $pvListID){
//the user is subscribed
$inPVList = true;
$converted_result = ($inPVList) ? 'true' : 'false';
}
}
}
$return[] = array(
"status" => $converted_result,
"list" => $listName
);
return json_encode($return);
}
return $return;
}