我正在尝试使用ajax将数组selectedItems
发送到我的数据库但由于某种原因这对我不起作用。
每次点击拨打deposit()
的按钮,我都会先收到警报:
“谢谢,我们会尽快向您发送交易请求!”
然后我收到警报:
“出了问题,请再试一次。”
因为$_POST["setDeposit"]
未设置。
的Javascript:
function deposit() {
var selectedItems = ["Banana", "Orange", "Apple"];
if (selectedItems.length !== 0) {
$.ajax({
type: "POST",
url: 'http://csgodonut.com/home/depositdone',
data:{setDeposit:selectedItems},
success:function() {
alert("Thank you, We will send you a trade request as soon as possible!");
}
});
} else {
alert("Please select atleast one item.");
}
}
控制器:
public function depositdone() {
if(isset($_POST['setDeposit'])) {
$array = $_POST['setDeposit'];
$depositItems = implode("<>", $array);
$steamid = $steamprofile["steamid"];
$tradeurl = $this->home_model->get_trade_url($steamid);
if ($tradeurl !== NULL) {
$this->home_model->add_deposit($tradeurl, $steamid, $depositItems);
header("Location:/home");
} else {
echo '<script>alert("You don\'t have a Tradelink set, please click on your name to set it!");</script>';
header("Location:/home");
}
} else {
echo "<script>
if (confirm('something went wrong, please try again.')) {
window.location.replace('http://csgodonut.com/home');
} else {
window.location.replace('http://csgodonut.com/home');
}
</script>";
}
}
答案 0 :(得分:0)
你没有在失败时返回任何东西。
将ajax函数作为调用的错误函数。
请查看调整后的样本。如果你掩饰错误,就无法解决错误。
我还建议在服务器端进行一些日志记录,以帮助诊断问题。
$.ajax({
type: "POST",
url: 'http://csgodonut.com/home/depositdone',
data:{setDeposit:selectedItems},
success:function() {
alert("Thank you, We will send you a trade request as soon as possible!");
},
error: function (xhr, ajaxOptions, theError) {
alert(xhr.status);
alert(theError);
}
});