返回通过运行PHP错误。 AJAX? [英] Return errors from PHP run via. AJAX?

查看:117
本文介绍了返回通过运行PHP错误。 AJAX?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让PHP如果PHP脚本某处失败,则返回一个AJAX错误code? 我下面的教程,并在我的PHP类型这样的:

Is there a way to get PHP to return an AJAX error code if the PHP script fails somewhere? I was following a tutorial and typed this in to my PHP:


$return['error'] = true;
$return['msg'] = "Could not connect to DB";

和一切都很好,直到我意识到这是JSON数据。有没有办法恢复使用标准的$ _ POST错误和返回的HTML数据(如,引发jQuery的AJAX 错误:事件

And all was well, until I realised it was JSON data. Is there a way to return errors using standard $_POST and returned HTML data (as in, trigger jQuery's AJAX error: event?

推荐答案

我不知道jQuery的,但如果它区分成功与不成功之间(HTTP 200 OK与HTTP!= 200)Ajax请求,您可能希望你的PHP脚本的HTTP code不等于200回应:

I don't know about jQuery, but if it distinguishes between successful and unsuccessful (HTTP 200 OK vs. HTTP != 200) Ajax requests, you might want your PHP script respond with an HTTP code not equal to 200:

if ($everything_is_ok)
    {
        header('Content-Type: application/json');
        print json_encode($result);
    }
else
    {
        header('HTTP/1.1 500 Internal Server Booboo');
        header('Content-Type: application/json; charset=UTF-8');
        die(json_encode(array('message' => 'ERROR', 'code' => 1337)));
    }

这篇关于返回通过运行PHP错误。 AJAX?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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