Ajax无法正常工作 [英] Ajax not working properly

查看:73
本文介绍了Ajax无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与我同住,我的JavaScript有点生锈.因此,我尝试使用ajax对PHP文件的调用并为其提供计划类型,然后对其进行有意义的检查,以查看是否允许的插槽小于用于该插槽的某些插槽,然后返回true或false.计划.这是XHTML中的表单.

Bear with me I'm my javascript is a little rusty. So I'm trying to use a call by ajax to a PHP file and give it a plan type then make sense of it check to see if it then return a true or false if some allowed slots are less than some slots used up for the plan. Here is the Form in XHTML.

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

在同一文件上.(< PLAN CHOICE>)被解析为当前计划.

On the same file. The ( < PLAN CHOICE > ) gets parsed out to the current plan.

<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看起来像这样.

My PHP for the ajax response looks like this.

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

如果您有任何建议,那就差不多了.

And that's pretty much it if you have any suggestions please let me know.

所以我尝试了另一种方式.

So I've tried to do it another way.

      $(".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]

The alert tells me this [object XMLHttpRequest]

有什么建议吗?

推荐答案

$.ajax()异步返回结果.使用链接到 $.ajax()调用的 .then()来执行基于响应的任务

$.ajax() returns results asynchronously. Use .then() chained to $.ajax() call to perform task based on response

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

这篇关于Ajax无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆