2013-01-23 37 views
2

我請求從URL的JSON數據,但它給錯誤的獲取JSON從URL給出錯誤[對象對象]沒有找到

GET HTTP://本地主機:10560/[對象%20Object] 404(未找到)

var mydata; 
$.getJSON({ 
    url: 'http://free.worldweatheronline.com/feed/weather.ashx?q=City,Country&callback=?&format=json&num_of_days=2&key=1111111111111111', 
    dataType: 'json', 
    success: function (data) { 
     mydata = data; 
     console.log(mydata); 
    } 
}); 

如何獲取json文件並解析它?

+2

在你的代碼中,Url是http://free.worldweatheronline.com/feed/weather。 ashx雖然在錯誤它說http:// localhost:10560/[object%20Object] 你確定,你沒有丟失任何東西... –

+2

看起來你已經發布了錯誤的ajax調用 – Johan

回答

4

你jQuery.getJSON()的使用是不正確,文檔:http://api.jquery.com/jQuery.getJSON/

二者必選其一:

var mydata; 
$.getJSON("http://free.worldweatheronline.com/feed/weather.ashx?q=City,Country&callback=?&format=json&num_of_days=2&key=1111111111111111",function(data){ 
    console.log(data) 
}) 

OR:

var mydata; 
$.ajax({ 
    url: 'http://free.worldweatheronline.com/feed/weather.ashx?q=City,Country&callback=?&format=json&num_of_days=2&key=1111111111111111', 
    dataType: 'jsonp', 
    success: function (data) { 
     mydata = data; 
     console.log(mydata); 
    } 
});