13

努力将 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"); });
4

3 回答 3

13

我认为你让它太复杂了:)

 var JSON;

 $.getJSON('example.json', function(response){
       JSON = response;
       alert(JSON.property);
 })
 //feel free to use chained handlers, or even make custom events out of them!
 .success(function() { alert("second success"); })
 .error(function() { alert("error"); })
 .complete(function() { alert("complete"); });

getJSON 函数会自动将您的响应转换为正确的 JSON 对象。无需解析。

您提到您正在到处使用这些数据,因此您必须等待 ajax 调用完成才能访问数据。这意味着要么将整个应用程序包装在getJSON回调中。或者使用自定义事件来确定:

 var JSON;

 $(window).on('JSONready', function(){
       alert(JSON.property);
 });

 $.getJSON('example.json', function(response){
       JSON = response;
       $(window).trigger('JSONready');
 });

 $('#elem').on('click', function(){
       //event likely to take place after ajax call has transpired
       //it would still be better to assign this listener in a callback, 
       //but you can get away with not doing it, if you put in a catch
       if(JSON){
           alert(JSON.property);
       }          
 });

编辑

经过快速实时调试后,数据不可用的真正原因是:使用 JSON 的 javascript 位于一个文件中,包括执行调用的内联 javascript 的页面文档 NORTH。结果 JSON 不是一个全局变量,并且作用域阻止了它的使用。如果你真的需要一个全局变量,以便它可以与内联 JS 以及任何和所有包含的 js 文件一起使用,你可以这样做:

  (function(){
      var limitedScopeVariable = 25;
      window.globalScopeVariable = 30;
  })();

  $(function(){
       alert(globalScopeVariable); //works!
       alert(limitedScopeVariable); //fails!
  });

编辑 2

自 jQuery 3.0 起,回调函数有所不同:自 jQuery 3.0 起,jqXHR.success()、jqXHR.error() 和 jqXHR.complete() 回调方法被删除。您可以改用 jqXHR.done()、jqXHR.fail() 和 jqXHR.always()

来自@mario-lurig 的评论

于 2012-06-12T22:30:44.237 回答
3

json 数据被传递给 $.getJSON 的回调函数。所以这会起作用:

var jqxhr;

$.getJSON("example.json", function(data) {
  jqxhr = data;
});

// alert(jqxhr.property);
// caution: this won't work immediately on load, since the ajax call runs asynchronously and hasn't finished at that time

// it should be available at a later time, like a click event
$('a#something').click(function(){
     if(jqxhr){
          alert(jqxhr.property);
     }else{
          alert('getJSON not yet complete or failed');
     }
});
于 2012-06-12T22:33:31.367 回答
0

我认为这将是您正在寻找的,您正在尝试访问从您的调用返回的数据,而不是调用者对象本身。在您的示例中, jqxhr 是处理 JSON 调用而不是数据的对象。所以,

$.getJSON("example.json", function(data) {
  yourDATA = data;
})

//Use your data here
alert(yourDATA.aProperty);

此页面上的第一个示例与我解释的类似。

于 2012-06-12T22:33:37.057 回答