我正在检查是否存在一个对象属性,其中包含一个保存有问题的属性名称的变量.
var myObj;
myObj.prop = "exists";
var myProp = "p"+"r"+"o"+"p";
if(myObj.myProp){
alert("yes, i have that property");
};
Run Code Online (Sandbox Code Playgroud)
这是undefined因为它正在寻找,myObj.myProp但我希望它来检查myObj.prop
努力将json从URL上的文件(myData.json)加载到对象中,以便我可以访问属性值.
- 数据立即加载,我在应用程序中需要它很多.
- 我将在整个应用程序中访问数据,而不仅仅是在数据加载后立即发生的一个函数的一部分.
- 我确保我文件中的数据格式正确json.
按照jquery API上的示例,我不应该做一些简单的事情:
警报(jqxhr.myProperty);
并获得价值?我在这里错过了什么一步?我试过运行eval和各种各样的东西
var myObj = JSON.parse(jqxhr);
无济于事.
求求你了,谢谢你.
// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.getJSON("example.json", function() {
alert("success");
})
.success(function() { alert("second success"); })
.error(function() { alert("error"); })
.complete(function() { alert("complete"); });
// perform other work here ...
// Set another completion function for the request above
jqxhr.complete(function(){ alert("second complete"); });
Run Code Online (Sandbox Code Playgroud)