2013-01-18 48 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);