2014-03-04 110 views
0

我开发了使用JQuery获取JSON数据的工具,但问题是某些变量不会放置它们的值,并且我只能使用变量名。此代码为什么我不能在JQuery中获取变量值

$(document).ready(function(){ 
$('.HTML').each(function(){ 
var call = $.getJSON('http://test-khamsat-support-gig.blogspot.com/feeds/posts/default/-/Break?max-results=10&orderby=published&alt=json-in-script&callback=?',function(data){ 
var cont = data.version; 
    }); 

    var th = $(this); 
    var b = th.html(), 
      a = b.match(/[^[\]]+(?=])/g); 
      if(a){ 
      if(a[2] == 'one'){ 
       th.append("<h1>'+cont+'</h1>") 
      } 
      } 
    }); 
    }); 

,因为它里面的内容我blog饲料代码将只是在我的博客上运行

+1

是一个跨域请求? –

+3

首先,在ajax请求中,你不能这样做 - http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call –

+0

第二,因为'cont'是一个变量'th.append(“

”+ cont +“

”)' –

回答

4

尝试

$(document).ready(function() { 
    $('.HTML').each(function() { 
     //store the reference to the element to use inside the callback 
     var th = $(this); 
     $.getJSON('http://test-khamsat-support-gig.blogspot.com/feeds/posts/default/-/Break?max-results=10&orderby=published&alt=json-in-script&callback=?', function (data) { 
      var cont = data.version; 

      //move this inside the ajax callback to support async nature 
      var b = th.html(), 
       a = b.match(/[^[\]]+(?=])/g); 
      if (a) { 
       if (a[2] == 'one') { 
        //use proper string concatenation here 
        th.append('<h1>' + cont + '</h1>') 
       } 
      } 
     }); 
    }); 
}); 
+0

非常感谢你,它非常棒。当我能够做到时(6分钟后),我会选择你的答案作为正确答案。 – abdel

相关问题