2017-07-28 71 views
0

我試圖將模板中的JavaScript變量發送到名爲'getMemberships'的函數Page.php,但在嘗試獲取它時遇到了很多麻煩工作。SilverStripe - JavaScript變量未成功通過AJAX發送到Page.php

我知道JavaScript函數ssCallBack可以工作並從SharpSpring獲取用戶信息。我知道getMemberships功能,如果你硬編碼在這裏的電子郵件作品:

$lists = $this->sharpSpringService->makeCall('getListMemberships', [ 
    'emailAddress' => '[email protected]', 
]); 

我知道,你可以使用AJAX,這是我設置和使用各種設置(試過的dataType試圖提交的JavaScript數據到PHP:「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; 

} 

回答

1

你想postVar('contactemail')postVars()返回所有的post變量。

+0

使用postVar()也不起作用。它只是返回一個空值,這是不正確的。 –

+0

我想出了這個問題。我測試的電子郵件實際上並不是我在getMemberships代碼中引用的訂閱列表的一部分,而且長話短說,這些東西已經在糟everything一切。 –