javascript 类型错误:data.forEach 不是函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53275405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 10:09:36  来源:igfitidea点击:

TypeError: data.forEach is not a function

javascriptjqueryhtmldjango

提问by absolutelydevastated

This is my code:

这是我的代码:

$.ajax({
    url: "some_url/",
    type: "GET",
    dataType: "json",
    success: function(data){
        console.log(data);
        data.forEach(function(element){
            console.log(element);
        });
    }
});

I get the error that for each does not work on the datavariable. However, when I log datato the console, I get

我得到的错误是每个都不适用于data变量。但是,当我登录data到控制台时,我得到

[{"model": "app.mdl", "pk": 1, "fields": {"name": "test", "rank": 1}}]

[{"model": "app.mdl", "pk": 1, "fields": {"name": "test", "rank": 1}}]

This is clearly an array and iterable, so I don't get what exactly is wrong.

这显然是一个数组并且是可迭代的,所以我不明白到底出了什么问题。

EDIT: datais returned via JsonResponsein Django.

编辑:data通过JsonResponseDjango返回。

回答by Mamun

I believe datais a JSON string. Since forEach()is a array function and you are trying to implement it on the JSON string it throws the error:

我相信数据是一个 JSON 字符串。由于forEach()是一个数组函数,而您试图在 JSON 字符串上实现它,它会引发错误:

"Uncaught TypeError: data.forEach is not a function"

“未捕获的类型错误:data.forEach 不是函数”

You have to parse the data with JSON.parse()before using forEach():

您必须JSON.parse()在使用之前解析数据forEach()

The JSON.parse()method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.

JSON.parse()方法解析 JSON 字符串,构造字符串描述的 JavaScript 值或对象。可以提供一个可选的 reviver 函数来在结果对象返回之前对其执行转换。

data = JSON.parse(data);

Demo:

演示:

var data = JSON.stringify([{"model": "app.mdl", "pk": 1, "fields": {"name": "test", "rank": 1}}]);
data = JSON.parse(data);
data.forEach(function(element){
    console.log(element);
});

So the successshould be:

所以成功应该是:

success: function(data){
    data = JSON.parse(data);
    console.log(data);
    data.forEach(function(element){
        console.log(element);
    });
}

回答by Negi Rox

just check for either it is string or JSOn array

只需检查它是字符串还是 JSOn 数组

if(typeof(data) === "string"){data = JSON.parse(data)}
 data.forEach(function(element){
            console.log(element);
        });

回答by codejockie

It is a case where your dataresponse looks like an array but it is a string. If you have access to the API you're connecting to you can ensure the response it sends out is an array but if not simply parsing the dataresponse using JSON.parse()should do the trick.

在这种情况下,您的data响应看起来像一个数组,但它是一个字符串。如果您有权访问要连接的 API,则可以确保它发出的响应是一个数组,但如果不是简单地解析data响应,则使用JSON.parse()应该可以解决问题。

回答by Meh

Change your success function to this, the JSON.parse()function is required to iterate over the JSON string:

将您的成功函数更改为此,该JSON.parse()函数需要迭代 JSON 字符串:

success: function(data){
        data = JSON.parse(data);
        console.log(data);
        data.forEach(function(element){
            console.log(element);
        });

回答by Anjana G

Just check if the data is JSON string.

只需检查数据是否为 ​​JSON 字符串。

data = "[{"model":"app.mdl","pk":1,"fields":{"name":"test","rank":1}}]"

if yes, the you have to do JSON.parse(data) and do forEach on it.

如果是,您必须执行 JSON.parse(data) 并在其上执行 forEach。