忍耐与我我是我的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对象]
有什么建议?
那么你的问题是什么? – epascarello
'成功:成功=数据'是错误的你应该有一个回调执行 – epascarello
https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – epascarello