【发布时间】:2015-08-18 09:18:45
【问题描述】:
你好 StackOverflow 上的朋友:
我正在努力寻找导致嵌套 jQuery 函数返回“未定义”或“[object Object]”值的原因。我已经注释了第一个函数的位置,其中包含函数调用。从我对这个主题的研究中可以看出,在这个令人困惑的问题上,我并不孤单。根据我在第二个“getAttributeValue”函数内的嵌套 jQuery 函数中使用的代码位,我得到不同的结果。 Stackoverflow 帮助程序可以看到我一直在尝试的代码的各种排列,但被注释掉以进行调试等。“警报”方法以我想要的方式返回完美清晰的数据。其他人什么都不返回,“未定义”或“[object Object]”,但从来没有我想要的完美清晰数据。我已经有足够长的时间知道我需要帮助。
数据源是提供 JSON 数据的 web api/web 服务。
代码如下:
//# This is the primary function that gathers data from a web api / web service:
$(document).ready(function () {
$("#loadData").click(function () {
$.get(url, function (data) {
var myHTMLDataTable = "<table class='table table-striped table-hover'>";
myHTMLDataTable = myHTMLDataTable + "<tr><th>WebId</th><th>Attributes</th><th>Values</th></tr>";
for (i = 0; i < data.Items.length; i++) {
myHTMLDataTable = myHTMLDataTable + "<tr>";
myHTMLDataTable = myHTMLDataTable + "<td class='col-sm-1'>" + (data.Items[i].WebId) + "</td>";
myHTMLDataTable = myHTMLDataTable + "<td class='col-sm-1'>" + (data.Items[i].Name) + "</td>";
myHTMLDataTable = myHTMLDataTable + "<td class='col-sm-1'>"+ getAttributeValue(data.Items[i].WebId) + "</td>"; //~ function call here
myHTMLDataTable = myHTMLDataTable + "</tr>";
}
myHTMLDataTable = myHTMLDataTable + "</table>";
document.getElementById("myData").innerHTML = myHTMLDataTable;
});
});
});
//# This function returns a current value and uom for the target attribute:
function getAttributeValue(attributeWebId) {
var theEnvelopePlease = ""; //~ clear the variable
var atrbUrl = "https://<<The Target Web API Root Address>>";
var atrbPrefix = "streams/";
var atrbWebId = attributeWebId;
var atrbExtension = "/value";
//~ Concatenate the URL used to make the request
atrbUrl = atrbUrl + atrbPrefix + atrbWebId + atrbExtension;
// theEnvelopePlease = $.getJSON(atrbUrl, function (data) { data.Value });
return $.getJSON(atrbUrl, function (data) {
//return (data.Value) + " " + (data.UnitsAbbreviation);
//alert(data.Value + " " + data.UnitsAbbreviation);
//theEnvelopePlease = theEnvelopePlease + (data.Value) + " " + (data.UnitsAbbreviation);
(data.Value) + " " + (data.UnitsAbbreviation);
});
// return theEnvelopePlease;
}
嵌套的 jQuery 函数正在检索的 JSON 数据如下所示:
{
"Timestamp": "2015-06-03T22:22:00Z",
"Value": 89.660293579101563,
"UnitsAbbreviation": "%",
"Good": true,
"Questionable": false,
"Substituted": false
}
也许有一位 JQuery 专家对此进行审查,他很容易看到我的错误。非常感谢您的帮助。
更新:因为有人要求我想显示我在使用 console.log 时得到的结果:
更新:嗨@IronFlare:谢谢你的建议。我用修改后的功能代码尝试了你的建议如下:
function getAttributeValue(attributeWebId) {
var theEnvelopePlease; //~ clear the variable
var atrbUrl = "https://<<The Target Web API Root Address>>";
var atrbPrefix = "streams/";
var atrbWebId = attributeWebId;
var atrbExtension = "/value";
//~ Concatonate the URL used to make the request
atrbUrl = atrbUrl + atrbPrefix + atrbWebId + atrbExtension;
$.getJSON(atrbUrl, function (data) {
theEnvelopePlease = (data.Value) + " " + (data.UnitsAbbreviation);
});
return theEnvelopePlease;
}
结果数据是'undefined' (:-c)
================================================ ========
您好@IronFlare,感谢您的建议。我修改了函数如下:
function getAttributeValue(attributeWebId) {
var theEnvelopePlease; //~ clear the variable
var atrbUrl = "https://<<The Target Web API Root Address>>";
var atrbPrefix = "streams/";
var atrbWebId = attributeWebId;
var atrbExtension = "/value";
//~ Concatenate the URL used to make the request
atrbUrl = atrbUrl + atrbPrefix + atrbWebId + atrbExtension;
$.getJSON(atrbUrl, function (data) {
theEnvelopePlease = (data.Value) + " " + (data.UnitsAbbreviation);
console.log(data);
});
return theEnvelopePlease;
}
这是结果的视图:
================================================ =====
更新:对于那些关注这个帖子的人,我尝试了这种排列:
//# Stackoverflow permutation 03:
function getAttributeValue(attributeWebId) {
var theEnvelopePlease; //~ clear the variable
var atrbUrl = "https://<<The Target Web API Root Address>>";
var atrbPrefix = "streams/";
var atrbWebId = attributeWebId;
var atrbExtension = "/value";
//~ Concatenate the URL used to make the request
atrbUrl = atrbUrl + atrbPrefix + atrbWebId + atrbExtension;
theEnvelopePlease = $.getJSON(atrbUrl, function (data) {
console.log(data.Value) + " " + (data.UnitsAbbreviation);
return (data.Value) + " " + (data.UnitsAbbreviation);
});
return theEnvelopePlease;
}
这是结果的视图:
【问题讨论】:
-
你能试试把 console.log 代替 alert 看看它在打印什么吗?
-
嗨@Sushil:是的,已经尝试过console.log,它也报告了完美的数据。
-
关于您的更新:嗯。将
console.log(data);添加到$.getJSON回调中,并将打印到控制台的对象添加到您的问题中。 -
对于@IronFlare,我很高兴地报告说,我终于解决了“未定义”或“[object Object]”返回值之谜,并在此发布了我的答案。
标签: javascript jquery web-services getjson webservice-client