【解决】Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse

ajax获取不到数据:
问题描述:
VM379:1 Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (


原因分析:
1、后端返回json数据格式错误
2、后端返回json数据方法不对

解决方案:
PHP:

  return json_encode(array(
         'status' => 3,
         'message' => 'ID不正确'
     ));

改为:

echo '{"status": 3, "mesg": "ID不正确" }';

注意:JSON字符串 应该用 英文双引号包裹

问题解决

 // ajax
    function del_cate(id, pid) {
      if (window.XMLHttpRequest) {
        var xmrt = new XMLHttpRequest();
      } else {
        var xmrt = new ActiveXObject('Microsoft.XMLHTTP');
      }

      xmrt.onreadystatechange = function () {
        if (xmrt.readyState == 4 && xmrt.status == 200) {
          console.log(xmrt);
          console.log(JSON.parse(xmrt.responseText));
          var status = JSON.parse(xmrt.responseText);

          if (status.status == 1) {
            alert('删除成功!');
            window.location.href = 'cate.php';
          } else if (status.status == 0) {
            alert('删除失败!');
            window.location.href = 'cate.php';
          } else {
            alert('删除失败!');
            window.location.href = 'cate.php';
          }
        }
      }
      xmrt.open('DELETE', './del_cate.php?id=' + id + '&pid=' + pid, true);
      xmrt.send();
    }

在这里插入图片描述

posted on 2021-11-12 17:01  漫思  阅读(3762)  评论(0编辑  收藏  举报

导航