2017-08-11 146 views
1

忍耐與我我是我的JavaScript有點生疏。所以我試圖使用ajax調用一個PHP文件,並給它一個計劃類型,然後檢查它是否檢查,看它是否返回true或false,如果某些允許的插槽少於某些插槽用完計劃。這是XHTML中的表單。Ajax無法正常工作

<form method="post" action="/membership-change-success" id="PaymentForm"> 
    <input type="hidden" name="planChosen" id="planChosen" value="" /> 
</form> 

在同一個文件上。 (< PLAN CHOICE>)被解析爲當前的計劃。

<script> 
    var hash = window.location.hash; 
    var currentPlan = "(< PLAN CHOICE >)"; 
    $(".planChoice").click(function(event){ 
      var isGood=confirm('Are you sure you want to change your plan?'); 
      var success; 
      $("#planChosen").val($(this).data("plan")); 
      $.ajax({ 
       url: '/ajax/planCheck.php', 
       type: "POST", 
       dataType: 'json', 
       data: ({plan: $(this).data("plan")}), 
       success: function (data) { //This is what is not working I can't get it to return true 
        success = data; 
       } 
      }); 
      if(success) { 
       if (isGood) { 
        $("#PaymentForm").submit(); 
       } 
       window.location = '/membership-change-success'; 
      } else { 
       alert('Please make sure you deactivate your listings to the appropriate amount before you Downgrade.') 
      } 
     }); 

我的PHP的Ajax響應看起來像這樣。

<?php 

require ('../includes/common.php'); 
include_once ('../includes/db-common.php'); 
require ('../includes/config.php'); 

$membership = new membership($dbobject); 
$listing = new listing($dbobject); 
$totalAvailableListings = ($membership->get_listingsAmount($_POST['plan'])); 
if($totalAvailableListings>=$listing->get_active_listings($user->id)){ 
    echo json_encode(true); // I've tried with out jason_encode too 
} else { 
    echo json_encode(false); 
} 

而這就是如果你有任何建議,請讓我知道。

所以我試圖以另一種方式做到這一點。

 $(".planChoice").click(function (event) { 
      var isGood = confirm('Are you sure you want to change your plan?'); 
      var success; 

      $("#planChosen").val($(this).data("plan")); 
      if (false) { 
       if (isGood) { 
        $("#PaymentForm").submit(); 
        alert('you did it'); 
       } 
      } else { 
       alert(isSuccessful($(this).data("plan"))); 
       //alert('Please make sure you deactivate your listings to the appropriate amount before you downgrade.'); 
      } 
     }); 

,我有一個Ajax功能

function isSuccessful(plan) { 
     return $.ajax({ 
      url: '/ajax/planCheck.php', 
      type: "POST", 
      dataType: 'json', 
      data: {plan: plan} 
     }); 
    } 

警報告訴我這[XmlHttpRequest對象]

有什麼建議?

+0

那麼你的問題是什麼? – epascarello

+3

'成功:成功=數據'是錯誤的你應該有一個回調執行 – epascarello

+0

https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – epascarello

回答

3

$.ajax()異步返回結果。使用.then()鏈接到$.ajax()呼叫基於響應執行任務

 $.ajax({ 
      url: '/ajax/planCheck.php', 
      type: "POST", 
      dataType: 'json', 
      data: {plan: $(this).data("plan")} 
     }) 
     .then(function(success) { 
      if (success) { 
       $("#PaymentForm").submit(); 
      } 
      // if `form` is submitted why do we need to set `.location`? 
      // window.location = '/membership-change-success'; 
     } else { 
      alert('Please make sure you deactivate your listings to the appropriate amount before you Downgrade.') 
     } 
     }, function err(jqxhr, textStatus, errorThrown) { 
      console.log(errorThrow) 
     }) 
+0

這個問題可能是由於這樣一個事實,即雖然在代碼中使用了$(「。planChoice」)。click ...',但類'plainChoice'沒有dom元素,並且裏面還有'$(this)' ajax – brk

+0

@brk這是可能的。 '$(this)'會引用被單擊的元素 - 如果元素存在 - 如果不存在,那麼我們甚至在到達'$ .ajax()'之前在'jQuery()'調用中發生錯誤。答案是「否」https://jsfiddle.net/39Ltsg3s/ – guest271314

+0

@brk https://stackoverflow.com/questions/45639894/how-to-catch-when-a-selector-is-passed-to-jquery -that-does-exist-in-docume – guest271314

0

您應該使用以下格式爲您的Ajax調用

$.ajax({ 
    url: '/ajax/planCheck.php', 
    type: "POST", 
    dataType: 'json', 
    data: ({plan: $(this).data("plan")}), 
    success: success = data 
    }) 
    .done(function(response) { 
     if(success) { 
      if (isGood) { 
       $("#PaymentForm").submit(); 
      } 
      window.location = '/membership-change-success'; 
     } 
     else { 
      alert('Please make sure you deactivate your listings to the 
      appropriate amount before you Downgrade.') 
     } 
    }); 

中,.done()子句確保你後執行代碼ajax調用完成並獲得響應。

+0

我'我嘗試了.done和.then響應,並且我的IDE提示未解決的函數或方法done()/ then() – OverBakedToast

+0

現在,當我嘗試返回ajax函數時,我得到[object XMLHttpRequest]。 – OverBakedToast

+0

你可以顯示你包含jquery腳本的地方嗎? 類似於 mandy1339