2014-04-20 88 views
-1

這是我的代碼。我想要做的就是記錄數據並能夠使用json數據,但它不起作用。getJSON不會工作

$(document).ready(function(){ 
    $.getJSON("http://removed_for_privacy.me/exercises.json", function(data) { 
     console.log(data); 
     alert(1); 
}); 
}); 

這不記錄數據,它甚至不運行警報(1)。這是跑掉服務器,有什麼想法?

+1

http://en.wikipedia.org/wiki/Same -origin_policy –

+0

它們位於同一個文件夾中的同一臺服務器上 – Ghozt12

+1

@ user3381694您確定exercise.json是有效的JSON嗎? –

回答

0

你的問題是getJson不會返回錯誤,請使用普通AJAX代替,

$.ajax({ 
    url: 'http://removed_for_privacy.me/exercises.json', 
    dataType: 'json', 
    success: function(data) { 
     console.log(data); 
    }, 
    error: function(data) { 
     console.log(data); 
    } 
    }); 

,或者使用.fail

$.getJSON("http://removed_for_privacy.me/exercises.json", function(data) { 
     console.log(data); 
     alert(1); 
}) 
.fail(function(jqxhr){ 
    alert(jqxhr.responseText); 
}); 
+0

thnx @Matt - 更新爲.fail也 –

+0

這工作,你知道爲什麼這個工程和getJSON不? – Ghozt12

+0

你的工作意味着什麼? @ user3381694 –