我正在调用服务以获取基于国家/地区的语言。当它返回多种语言时,我的以下代码工作正常..但是当它只返回一条记录时,输出不正确(作为“未定义”出现..请咨询。
这是我的代码:
$.ajax({
type: 'POST',
url: "http://mymethod",
complete: function () { $.mobile.hidePageLoadingMsg(); },
dataType: "json",
contentType: 'application/json',
data: JSON.stringify({
"country": countryCode
}),
success: function (output, textStatus, jqXHR) {
$.each(output.geographyInfo, function (i, theItem) {
try {
languages.push("<option value='" + theItem.languageCode + "'>" + theItem.languageName + "</option>");
}
catch (error) {
alert(error);
}
});
$(languages.join('')).appendTo("#dpdLanguages").trigger("create");
}
});
这是json输出。
这是只有一种语言时的输出:
{"geographyInfo":{"active":"true","countryCode":"US","countryName":"UNITED STATES","instanceID":"1","languageCode":"EN","languageName" :"English","regionName":"North America"},"reasonDetails":null,"size":"1","status":"true"}
在多种情况下
{"geographyInfo":[{"active":"true","countryCode":"CA","countryName":"CANADA","instanceID":"1","languageCode":"EN","languageName" :"English","regionName":"North America"},{"active":"true","countryCode":"CA","countryName":"CANADA","instanceID":"1","languageCode ":"FR","languageName":"French","regionName":"North America"},{"active":"true","countryCode":"CA","countryName":"CANADA"," instanceID":"2","languageCode":"EN","languageName":"English","regionName":"Americas"}, {"active":"true","countryCode":"CA","countryName":"CANADA","instanceID":"2","languageCode":"FR"," languageName":"French","regionName":"Americas"}],"reasonDetails":null,"size":"4","status":"true"}
是的,udidu,我认为是这样..但是如何解决这个问题?