console.log(result)返回[object Object]。我如何获得result.name? [英] console.log(result) returns [object Object]. How do I get result.name?

查看:593
本文介绍了console.log(result)返回[object Object]。我如何获得result.name?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

有人可以解释如何 console.log 返回 id 名称来自结果

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

$.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.');
        }
    }
});


推荐答案

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

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

编辑:如果你打算获得id和其他结果对象的属性,你想看到控制台知道它是否在那里你可以检查 hasOwnProperty 并访问该属性,如果它存在:

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:

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
}

这篇关于console.log(result)返回[object Object]。我如何获得result.name?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆