2013-01-10 30 views
0

我試圖獲取數據,但以下腳本不起作用。你能幫我弄明白嗎?使用anyorigin在JavaScript中獲取JSON

function initialize() { 
$.getJSON('http://anyorigin.com/get?url=http%3A//www.wawhost.com/appProject/fetchmarker.php&callback=?', function (data) { 
localStorage.jsontext = data.contents; 
}); 
} 

localStorage.jsontext = localStorage.jsontext.replace('(', '{"temp":'); 
localStorage.jsontext = localStorage.jsontext.replace(')', '}'); 

obj = JSON.parse(localStorage.jsontext); 

for(var i=0;i<=5;i++) { 
document.write('<img src="' + obj.temp[i].image + '" />'); 
document.write(obj.temp[i].id); 
document.write(obj.temp[i].location); 
document.write('<br>'); 
} 

謝謝!

回答

0

你知道http://www.wawhost.com/appProject/fetchmarker.php提供了jsonp。

function initialize() { 
    $.getJSON('http://www.wawhost.com/appProject/fetchmarker.php?callback=?', function (data) { 
     for(var i=0;i<data.length;i++) { 
      document.write('<img src="' + data[i].image + '" />'); 
      document.write(data[i].id); 
      document.write(data[i].location); 
      document.write('<br>'); 
     } 
    }); 
} 
initialize(); 

http://jsfiddle.net/mowglisanu/BWfxw/

+0

你是最好的!非常感謝你! – alesko007