javascript - Ajax 无法正常工作

标签 javascript php ajax

请耐心等待,我的 javascript 有点生疏了。所以我试图通过 ajax 调用一个 PHP 文件并给它一个计划类型然后理解它检查它是否返回 true 或 false 如果一些允许的插槽少于一些用于计划。这是 XHTML 中的表单。

<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.')
          }
      });

我用于 ajax 响应的 PHP 看起来像这样。

    <?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}
      });
  }

警报告诉我这个 [object XMLHttpRequest]

有什么建议吗?

最佳答案

$.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)
      })

关于javascript - Ajax 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45639528/

相关文章:

javascript - 如何使用 Flask 从 javascript 获取变量

javascript - 如何将排序和匹配谷歌表格公式转换为应用程序脚本/javascript?

javascript - 当后端在 Flask 中运行时动态填充 html

jquery - 如何等待 ajax 调用完成然后再进行下一个调用?

ajax - 阐述想法运动ajax wicket元素

javascript - 使用 Applescript 查找两个常量之间的变量文本

javascript - 使用 RegEx 删除字符串中 "/"之后的空格

php - Revive Ad 无法升级

PHP:如何生成字符串的 hmac SHA1 签名?

PHP:将 MD5 哈希应用升级为 bycript。这安全吗?