2013-01-18 27 views
0

點擊我試圖通過URL從JSON獲取數據到<div>點擊獲取JSON數據到DIV

我的JSON返回一個單一的ID和內容,我試圖讓內容進入div #west-container

有人能告訴我我做錯了什麼嗎?

我的JSON在URL找到= [{"id":"2","title":"Generic Overview","content":"This is content here"}]

$('#jqxTree').bind('select', function (event) { 
var loadPages = jQuery.parseJSON(
     jQuery.ajax({ 
      url: 'university/article/3', 
      async: false, 
      dataType: 'json' 
     }).responseText 
    ); 

for(i=0; i < loadPages.length ; i++){ 
    var current = loadPages; 
    $("#west-container").load(current.content); 
} 
}); 
+0

爲什麼不使用[的getJSON(http://api.jquery.com/jQuery.getJSON/)? – Stefan

+0

爲什麼不使用'$ .ajax()'的成功函數? – Jai

+0

是這個命令jQuery.ajax({url:'university/article/3', async:false, dataType:'json' })。responseText alone you get this line?{「id」:「2 「,」標題「:」通用概覽「,」內容「:」這裏是內容「} – HiDd3N

回答

1

你可以試試這個:

$('#jqxTree').bind('select', function (event) { 
    jQuery.ajax({ 
     url: 'university/article/3', 
     async: false, 
     dataType: 'json', 
     success:function(data){ 
      $.each(data, function(i, item){ 
       $("#west-container").html(item.content); 
      });  //-----------------^^^^^^^^^^^^---This will fetch you the 
     }    //--------------------------------content from json 
    });    //-------------------------"content":"This is content here" 
}); 
0
$.getJSON('university/article/3', function(data) { 
// Do stuff here 

var oDiv = $('west-container').html(''); 

for(var i in data) { 
    oDiv.append(data[i].content); 
} 
}); 
0

使用

$.ajax({ 
     url: "url", 
     type: 'POST', 
     contentType: 'application/json; charset=utf-8', 
     data:json, 
     dataType:'text', 
     async: false, 
    }).done(function(data1) { 
     data=jq.parseJSON(data1); 
    }); 

爲了使用這些數據,使用 「data.id,data.title」 等

爲了將數據推送到div,請使用

$('#DivId').test(data.id);