Possible Duplicate:
Keeps saying result property is not defined. Why?
我正在解析xml,我想要的是解析后返回xml。问题是在我的 getResult()
中功能result
值为 undefined
。为什么以及如何让它发挥作用?
这是我的代码
var result = '';
var Xml = {
to : null,
from : null,
url : null,
init: function (fromaddress, toaddress, link) {
from = fromaddress;
to = toaddress;
url = link;
this.requestXml();
return this;
},
requestXml: function () {
$.ajax({
type: "GET",
url: url,
dataType: "xml",
success: this.parseXml
});
},
parseXml: function (xml) {
console.log('xml: ' + $(xml));
result = $(xml);
},
getResult: function () {
console.log('Result: ' + Xml.result); //<--- Here result has undefined value
return result;
}
};
最佳答案
Xml.result 从未被设置。也许我遗漏了一些东西,但是您需要在 parseXml 中执行此操作才能设置其结果字段:
this.result = $(xml);
并且在 getResult 中,您的 console.log 调用不应显示未定义。您甚至可以让它返回 this.result
。
另一件事是,在初始化程序中,您应该使用“this”关键字设置每个属性。
关于javascript - 解析 xml 后,结果未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13286177/