gpt4 book ai didi

javascript - SilverStripe - JavaScript 变量未通过 AJAX 成功发送到 Page.php

转载 作者:行者123 更新时间:2023-12-03 03:46:09 25 4
gpt4 key购买 nike

我正在尝试将模板中的 JavaScript 变量发送到 Page.php 中名为“getMemberships”的函数,但在尝试使其正常工作时遇到了很多麻烦。

我知道 JavaScript 函数 ssCallBack 确实可以工作并从 SharpSpring 获取用户信息。我知道如果您在此处对电子邮件进行硬编码,则 getMemberships 函数会起作用:

$lists = $this->sharpSpringService->makeCall('getListMemberships', [
'emailAddress' => 'youremail@site.com',
]);

我知道您可以使用 AJAX 将 JavaScript 数据提交到 PHP,我已经设置并尝试使用各种设置(尝试了 dataType: 'json'、dataType: 'jsonP'、dataType: '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;
}
}
}
});
}

Page.php

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;

}

最佳答案

您需要postVar('contactemail')postVars() 返回所有帖子变量。

关于javascript - SilverStripe - JavaScript 变量未通过 AJAX 成功发送到 Page.php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45382327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com