Ajax在php json_encode上显示错误

Ajax show error on php json_encode

本文关键字:显示 错误 encode php json Ajax      更新时间:2024-03-05

我正试图从一个由php文件填充的json数组中获取数据。数据成功传递到php文件,但返回错误。我尝试检查页面,它显示了网络元素中返回的数组,如图所示。网络检查元素,但ajax成功功能不起作用。如果数据是从php文件成功传递的,为什么不执行ajax成功函数
这是我的ajax代码:

var a = $(this).attr('idq');
$.ajax({
    type: "POST",
    url: "try.php",
    data: {
     queryy: a
    },
    dataType: "json",
    success: function(result) {
     var data = jQuery.parseJSON(result);
     $.each(data, function(index, value) {
         alert("successful");
     });
    },
    error: function(result) {
     alert("error");
    }
});

如果将dataType作为json,则不需要parseJSON。只需从代码中删除jQuery.parseJSON()即可。

dataType:"json"告诉jQuery将响应解析为JSON。

因此,您将一个实际对象传递给jQuery.parseJSON,这将不起的作用