doucan8521 2019-02-01 09:07
浏览 60

如何在AJAX中以不同方式处理从PHP发送的响应

I am trying to figure out that how can I handle different responses sent from PHP to AJAX differently (I am using toastr for nice message display on front end). Say someone tried to insert some value into the database and the value got inserted successfully, then i sent I message from PHP by

echo "Data inserted";

and this gets caught by ajax response and gets handled by

success: function(response) {
 toastr.success(response);
}

But I want that, when PHP sends a message something like

echo "Unable to insert data";

My AJAX handles it as an error and I want to use toastr.danger() instead of toastr.success().

How can I handle this situation ?

  • 写回答

3条回答 默认 最新

  • douxianji3367 2019-02-01 09:14
    关注

    As Jon Stirling said in comment, check HTTP status code (200: OK, 404: Not found, 500: Server error...), statusCode in $.ajax options.

    Or use error in $.ajax options (opposed to success options).

    Or (but not the best really), check for a word in your response with String.indexOf('yourWord')

    评论

报告相同问题?

悬赏问题

  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录