Uncaught TypeError: Cannot read property 'forEach' of null

Uncaught TypeError: Cannot read property 'forEach' of null

我在 ajax 响应中收到此错误:

    $.ajax({
        type: "GET",
        url: "/search",
        data: {
            s: value,
        },
        success: function (data, textStatus, jqXHR) {
            console.log('data is:', data);
            if (data != null) { 
                    jsonObject = JSON.parse(data);
                    jsonObject.forEach(function (data) { //error here
               // do stuff
            } else {
                console.log('null data received');
            }

但我总是得到这个错误:

data is: null
projects.js:23 Uncaught TypeError: Cannot read property 'forEach' of null
    at Object.success (projects.js:23)
    at c (jquery-3.5.1.min.js?1624781025:2)
    at Object.fireWith [as resolveWith] (jquery-3.5.1.min.js?1624781025:2)
    at l (jquery-3.5.1.min.js?1624781025:2)
    at XMLHttpRequest.<anonymous> (jquery-3.5.1.min.js?1624781025:2)

我的条件查找空数据有什么问题,我该如何解决?

错误说 jsonObjectnull

您正在测试 data 是否为 null

因此data必须null表示为JSON所以data就是字符串null.

("null" != null)