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

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

问题描述

我的脚本返回 [object Object] 作为 console.log(result) 的结果.

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

谁能解释一下如何让console.logresult返回idname?

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天全站免登陆