Ajax請求返回Error:200無資料的解決方法
先看程式碼
1 $.ajax({ 2type:"GET", 3url:"https://****/charts/data/genre2.json", 4dataType:"json", 5success:function(data){ 6if(data.errorCode==0){ 7console.log("成功,無資料"); 8console.log(data); 9}else{ 10console.log(data); 11createChart(data); 12} 13}, 14error:function(jqXHR){ 15console.log("Error: "+jqXHR.status); 16} 17});
報錯現象:頁面為空
解決方式:因為 返回200表示請求成功 ,已返回網頁;但是無資料,那麼可以檢查資料;
[ {'genre':'Sports','sold': 275 }, { 'genre': 'Strategy', 'sold': 115 }, { 'genre': 'Action', 'sold': 120 }, { 'genre': 'Shooter', 'sold': 350 }, { 'genre': 'Other', 'sold': 150 } ]
如上,這是要請求的資料, 將json內單引號改為雙引號 再測試即可;
參考部落格: https://blog.csdn.net/java_zhangshuai/article/details/80274510
[ {"genre":"Sports","sold": 275 }, { "genre": "Strategy", "sold": 115 }, { "genre": "Action", "sold": 120 }, { "genre": "Shooter", "sold": 350 }, { "genre": "Other", "sold": 150 } ]