console.log(结果)返回对象的对象。如何得到结果?(复制)

时间:2021-03-15 16:56:03

This question already has an answer here:

这个问题已经有了答案:

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

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

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

谁能解释一下怎样使用控制台吗?日志返回结果的id和名称?

$.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


30  

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


8  

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进行stringify,这使得调试更加容易。

#1


30  

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


8  

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进行stringify,这使得调试更加容易。