XmlHttpRequestを使用して別のドメインからJSONデータを読み取る次のコードが見つかりました。 JSONPを使わずに動作します。私はそれが不可能だと思った(http://en.wikipedia.org/wiki/Same_origin_policy)。それはなぜ機能するのですか?XmlHttpRequest - 他のドメインからJSONを読み取ることができますか?
<script type="text/javascript">
// Set up the http request object for AJAX calls
var http = false;
if(navigator.appName == "Microsoft Internet Explorer") http = new ActiveXObject("Microsoft.XMLHTTP");
else http = new XMLHttpRequest();
// Begin by getting partial codelist for all regions in the United Kingdom from the API in JSON format
http.open("GET", "http://www.nomisweb.co.uk/api/v01/dataset/nm_1_1/geography/2092957697TYPE480.def.sdmx.json", true);
http.onreadystatechange=function() {
if(http.readyState == 4 && http.status == 200) {
// Evaluate the JSON response
var jsonlist = eval("(" + http.responseText + ")");
// String to hold the html for area selection buttons
var mycodelist = '';
// Loop through each code in the codelist and build up buttons for the user to click
for(i = 0; i < jsonlist.structure.codelists.codelist[0].code.length; i++)
{
// Get the code value
var code = jsonlist.structure.codelists.codelist[0].code[i].value;
// Get the description value
var desc = jsonlist.structure.codelists.codelist[0].code[i].description.value;
// Construct the html for this area button
mycodelist += '<input type="button" onclick="getdata(' + code + ',\'' + desc + '\');" value="' + desc + '"><br>';
}
// Display the area selections in the "mylist" div
document.getElementById('mylist').innerHTML = mycodelist;
}
}
http.send(null); // Make the API request
</script>
これはChromeで動作します。
http://www2.esd.org.uk/betaesdmapping/1234.HTML
UPDATE
それはFF 3.6またはIE 7
マキシム
私は貼り付けたコードが動作している理由を説明してもらえますか? –
サーバ設定なしではありません:) – Jivings
CORSについての私の答えが更新されました。 – Jivings