我正在尝试使用jquery编写以下代码。这从网站读取json并创建一个我想要返回的字符串,但它总是返回“undefined”。我在这里错过了什么?
function getTenders(wardName){
output = "";
$.getJSON('http://192.168.1.105:3000/getTenders.json?name='+wardName, function (obj) {
//console.log(body); // Print the google web page.
for(var i=0; i < obj.length; i++){
var pid = "Project id: " + obj[i].id;
var status = "Status: " + obj[i].status;
var title = "Title: " + obj[i].title;
var cost = "Estimated Cost: Rs " + obj[i].estimated;
output += pid + "\n" + status + "\n" + title + "\n" + cost + "\n\n";
}
console.log("Output: "+ output) // This prints correct output
return output // Returning output here
});
}
// But this shows undefined
console.log(printTenders("Somevalue")) // This outputs undefined. How can that happen?