console.log(result)返回[object Object]。我如何获得result.name? [重复]

时间:2022-07-01 16:57:01

This question already has an answer here:

这个问题在这里已有答案:

My script is returning [object Object] as a result of console.log(result).

我的脚本由于console.log(结果)而返回[object Object]。

Can someone please explain how to have console.log return the id and name from result?

有人可以解释一下如何让console.log从结果中返回id和name吗?

$.ajaxSetup({ traditional: true });var uri = "";$("#enginesOuputWaiter").show();    $.ajax({    type: "GET",    url: uri,    dataType: "jsonp",    ContentType:'application/javascript',    data :{'text' : article},    error: function(result) {        $("#enginesOuputWaiter").hide();        if(result.statusText = 'success') {            console.log("ok");            console.log(result);        } else {            $("#enginesOuput").text('Invalid query.');        }    }});

2 个解决方案

#1


42  

Use console.log(JSON.stringify(result)) to get the JSON in a string format.

使用console.log(JSON.stringify(result))以字符串格式获取JSON。

EDIT: If your intention is to get the id and other properties from the result object and you want to see it console to know if its there then you can check with hasOwnProperty and access the property if it does exist:

编辑:如果你的目的是从结果对象中获取id和其他属性,并且你想看到控制台知道它是否存在,那么你可以检查hasOwnProperty并访问该属性(如果它存在):

var obj = {id : "007", name : "James Bond"};console.log(obj);                    // Object { id: "007", name: "James Bond" }console.log(JSON.stringify(obj));    //{"id":"007","name":"James Bond"}if (obj.hasOwnProperty("id")){    console.log(obj.id);             //007}

#2


13  

Try adding JSON.stringify(result) to convert the JS Object into a JSON string.

尝试添加JSON.stringify(result)将JS对象转换为JSON字符串。

From your code I can see you are logging the result in error which is called if the AJAX request fails, so I'm not sure how you'd go about accessing the id/name/etc. then (you are checking for success inside the error condition!).

从您的代码中我可以看到您正在记录错误结果,如果AJAX请求失败则调用该错误,因此我不确定您将如何访问id / name / etc.然后(你正在检查错误条件内的成功!)。

Note that if you use Chrome's console you should be able to browse through the object without having to stringify the JSON, which makes it easier to debug.

请注意,如果您使用Chrome的控制台,则应该能够浏览对象而无需对JSON进行字符串化,这样可以更轻松地进行调试。

#1


42  

Use console.log(JSON.stringify(result)) to get the JSON in a string format.

使用console.log(JSON.stringify(result))以字符串格式获取JSON。

EDIT: If your intention is to get the id and other properties from the result object and you want to see it console to know if its there then you can check with hasOwnProperty and access the property if it does exist:

编辑:如果你的目的是从结果对象中获取id和其他属性,并且你想看到控制台知道它是否存在,那么你可以检查hasOwnProperty并访问该属性(如果它存在):

var obj = {id : "007", name : "James Bond"};console.log(obj);                    // Object { id: "007", name: "James Bond" }console.log(JSON.stringify(obj));    //{"id":"007","name":"James Bond"}if (obj.hasOwnProperty("id")){    console.log(obj.id);             //007}

#2


13  

Try adding JSON.stringify(result) to convert the JS Object into a JSON string.

尝试添加JSON.stringify(result)将JS对象转换为JSON字符串。

From your code I can see you are logging the result in error which is called if the AJAX request fails, so I'm not sure how you'd go about accessing the id/name/etc. then (you are checking for success inside the error condition!).

从您的代码中我可以看到您正在记录错误结果,如果AJAX请求失败则调用该错误,因此我不确定您将如何访问id / name / etc.然后(你正在检查错误条件内的成功!)。

Note that if you use Chrome's console you should be able to browse through the object without having to stringify the JSON, which makes it easier to debug.

请注意,如果您使用Chrome的控制台,则应该能够浏览对象而无需对JSON进行字符串化,这样可以更轻松地进行调试。