Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何显示JSON解析错误?_Php_Ajax_Json - Fatal编程技术网

Php 如何显示JSON解析错误?

Php 如何显示JSON解析错误?,php,ajax,json,Php,Ajax,Json,我有一个前端登录页面,旨在解析JSON消息。其逻辑是: $(document).ready(function() { $('#form_login').ajaxForm(function(response) { $("#content").html(response.message); if(response.result == 0) { window.setTimeout(function() { window.location.href

我有一个前端登录页面,旨在解析JSON消息。其逻辑是:

    $(document).ready(function() {
  $('#form_login').ajaxForm(function(response) {
    $("#content").html(response.message);
    if(response.result == 0) {
      window.setTimeout(function() {
        window.location.href = response.lnkmember;
      }, 2000);
    }
  });
});
问题是,如果后端PHP中存在输入错误或错误,那么前端无法显示这些错误或通知用户。它看起来是挂着的,但当我在Chrome上打开开发工具时,我可以看到下面的网络数据确实被传输回来了

<br />
<b>Notice</b>:  Undefined variable: email_support in <b>technical\public_html\email.php</b> on line <b>57</b><br />
Mailer Error: You must provide at least one recipient email address.
{"result":"2", "message":"An internal error occurred. Our technical staff has been informed to fix this. Sorry for any inconvenience."}

注意:未定义变量:第57行technical\public\u html\email.php中的email\u支持
邮件程序错误:您必须至少提供一个收件人电子邮件地址。 {“结果”:“2”,“消息”:“发生内部错误。已通知我们的技术人员解决此问题。对于由此带来的不便,我们深表歉意。”
在后端,我使用了try-catch逻辑,并将在JSON回复中返回一个非零字段“result”。但是,如果php错误是在json消息的前面加上前缀,那么前端就不高兴了


最令人满意的解决方案是将PHP错误发送给技术支持,同时只向用户显示我的JSON消息。但我不知道如何做到这一点。那么您能帮我一下吗?

您需要显式捕获自己的错误消息,我将尝试将错误的JSON作为字符串传递回客户端:

    $(document).ready(function() {
        $('#form_login').ajaxForm(function(response) {

            if ( response.result == 2 ) {
               // handle error
               console.log(response.message);
               console.log(response.originalJSON);
            } else {

                $("#content").html(response.message);
                if(response.result == 0) {
                    window.setTimeout(function() {
                      window.location.href = response.lnkmember;
                    }, 2000);
                }
            }
        });
    });
与消息一起传递回发送的JSON字符串:

    {"result":"2", 
     "message":"An internal error occurred. Our technical staff has been informed to fix this. Sorry for any inconvenience."
     "originalJSON":" .... stringyfied JSON from the PHP ...." }
编辑 在PHP方面,您可以通过以下方式捕获JSON错误:

    $json_parsed = json_decode($json_string, true);

    if ( hasJSONerror() !== false )
    {
        // output json_string
        return;
    }
    ....

    function hasJSONerror()
    {
        // Define the possible JSON errors.
        $json_errors = array(
          JSON_ERROR_NONE => 'No error has occurred',
          JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
          JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
          JSON_ERROR_SYNTAX => 'Syntax error',
          JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded'
        );
        if ( json_last_error() != 0 )
        {

            return json_last_error();
        }
        return false;
    }

由于在PHP中解析响应中的警告或通知的实际困难(实际错误将导致500个响应头,这很容易被捕获),我个人对生产的理解是实际告诉PHP禁用此页面中显示的所有警告

因此,在这个PHP脚本的顶部,添加:

error_reporting(E_NONE);
ini_set('display_errors', 0);

这将禁用PHP对此页面的任何错误输出,从而允许您处理这些错误


请注意:最好确保在生产过程中不会出现此类警告!!!如果有一个模糊点,您觉得无法处理(不应该),您可以始终使用
@
符号使PHP静音。

那么,为什么您首先要访问PHP中未定义的变量呢?由于通知消息,响应是无效的JSON-不要相信它会被解析以访问
响应。消息
“这将禁用此页面的任何PHP错误输出,允许您处理它们。”---这是错误的<代码>显示错误设置与它们的捕获能力无关。答案中关于
@
的部分是有害的。@zerkms:问题不在于可捕获性-它表明返回的JSON包含错误代码和消息,但由于存在警告而未被解析。因此我的答案是,我只提到了你答案中的一个特定部分:“这将禁用PHP在这个页面上的任何错误输出,允许你处理它们。”这是错误的<代码>显示错误不允许或阻止处理某些内容。最好不要禁用错误输出,而只是修复它们。@zerkms:摘自PHP手册中的“display_errors”:“这决定了错误是应该作为输出的一部分打印到屏幕上,还是应该对用户隐藏。”。问题是对输出隐藏错误,允许JSON被解析——不是完全解析。嗯,“不打印”错误!=“处理”它。仅仅因为你隐藏了一些东西并不意味着你处理了它。“问题是如何从输出中隐藏错误”——不,不是。简单地隐藏它是一个愚蠢的解决方案。我们不应该访问未定义的变量,那么这个错误就永远不会发生。