2012-08-13 80 views
0

我想做一個Ajax調用服務器返回一個JSON對象,但它不工作...不知道什麼是錯的,請幫助...這裏是代碼:JQuery的Ajax調用拋出語法無效的標籤錯誤

$.ajax ({ 
     type : "GET", 
     url : "http://api.zero1.org/v1/artists", 
     data : "", 
     dataType : "jsonp" 
    }).done (function(msg){ 
     var artistNames = msg.artists[0].name; 

     for (var i = 0; i < artistNames.length; i++) { 
      console.log(artistNames[i]) 
      $('div#artists').html(function() { 
       return '<li><a href="details.html"><h3 class="ul-li-heading">' + artistName[i] + '</h3><p class="ul-li-desc">artist/location</p></a></li>' 
      }); 
     } 
    }) 

的JSON對象是像這樣:

{ 
    artists: [ 
       { 
       artistid: "5", 
       name: "Outdoor Urban Scene ", 
       bio: "", 
       programs: [ 
          1, 
          2, 
          3, 
          4 
          ] 
       }, 
       { 
       artistid: "87", 
       name: "Radames Ajna", 
       bio: "Technical development of Mobile Crash v2", 
       programs: [ 
          1, 
          2, 
          3, 
          4 
          ] 
       } 
      ] 
} 

回答

0

您正試圖使用​​不支持JSONP資源。服務器必須正確配置才能處理JSONP請求。

你可以寫一些自己的服務器端代碼來處理JSON和服務於它,或者你可以從Yahoo使用像HQL服務:

$.ajax({ 
    url: 'http://query.yahooapis.com/v1/public/yql', 
    data: { 
     q: 'select * from json where url="http://api.zero1.org/v1/artists"', 
     format: 'json' 
    }, 
    type: 'get', 
    dataType: 'jsonp' 
}) 
0

請儘量crossDomain選項添加到您的AJAX call.Your code must end with ;

$.ajax ({ 
     type : "GET", 
     url : "http://api.zero1.org/v1/artists", 
     data : "", 
     dataType : "jsonp", 
     crossDomain: true 
    }).done (function(msg){ 
     var artistNames = msg.artists[0].name; 

     for (var i = 0; i < artistNames.length; i++) { 
      console.log(artistNames[i]) 
      $('div#artists').html(function() { 
       return '<li><a href="details.html"><h3 class="ul-li-heading">' + artistName[i] + '</h3><p class="ul-li-desc">artist/location</p></a></li>' 
      }); 
     } 
    }); 
相關問題