du248227 2015-03-26 14:38
浏览 35

JQuery Ajax PHP中的错误和成功处理

how can i handle error and success messages in Ajax? I have an ajax call where my datas are saved into a db. My php script first checked if a user have x amount. if he has less then x then he should fire a alert (amount < x) Else insert into db.

my php file:

 ...
 .....
 if ($wallet_amount < "100") {
     $wa1 = 0;
     echo $wa1;
 } else {
     $inito = $connection->prepare("INSERT INTO bs (title, start, end, userid, typ, code) VALUES (:title, :start, :end, :pid, :col, :code)");
     $inito->bindValue(':pid', $pid, PDO::PARAM_INT);
     $inito->bindValue(':title', $title, PDO::PARAM_STR);
     $inito->bindValue(':start', $start, PDO::PARAM_STR);
     $inito->bindValue(':end', $end, PDO::PARAM_STR);
     $inito->bindValue(':col', $col, PDO::PARAM_STR);
     $inito->bindValue(':code', $code, PDO::PARAM_INT);
     $inito->execute();

     exit();        
} 

My js file:

$.ajax({
    url: 'add.php',
    data: {
        'title': $('#Name').val(), 
        'start': start, 
        'end': $('#End').val(), 
        'code': $('input[name="code"]:checked').val()
    },
    type: "POST",
    error: function () {
        alert('There was an error while adding events.');
    }
});

My first try goes wrong. I write something like that:

 success: function (response) {
            if (response === 0) {
                alert("Amount < X!");
            } else if (response === 1) {
                alert("Amount > X);
            }
        },
        error: function () {
            alert('There was an error while adding events.');
        }
  • 写回答

3条回答 默认 最新

  • dsgo31121 2015-03-26 14:44
    关注

    I guess in success block you have used a strict check ===, this checks the type and value both:

    success: function (response) {
            if (response === '0') { // update this 
                alert("Amount < X!");
            } else if (response === '1') {
                alert("Amount > X"); //<---quote is missing i guess it's just a typo.
            }
        },
        error: function () {
            alert('There was an error while adding events.');
        }
    

    Also i am not sure if this check } else if (response === '1') { would ever happen because i don't see if you get '1' in the response.

    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型