2017-08-11 152 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