我在这个CORS之间感到困惑??:-(
我只是请求你一点帮助。什么是使用 javascript CORS 的 XMLHttpRequest 的完整源代码。我如何从发送的服务器获取 json 数据。如果我可以使用地址栏访问 json 文件。我如何从中获取这些数据?
这是 Json 文件“Data.json”
{"JsonProjectIDResult": [{"_capacity": 15,"_description": "Meeting Room","_dev_default_view": 3,"_deviceID": 1,"_deviceName": "MobiTech","_deviceTypeID": 1,"_projectID": 1,"_roomID": 2,"_roomName": "Room2","_room_admin_mail": null}]}
这里我的 home.html 简单脚本
var resultDiv = document.getElementById("results");
var newsURL = "http://10.1.128.106/JsonDohaBackToTemplate/getProjectIDByMAC.svc/mac/10:95:84:11:00:05";
var e;
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
e = new XMLHttpRequest();
} else {
// code for IE6, IE5
e = new ActiveXObject("Microsoft.XMLHTTP");
}
e.onreadystatechange = function() {
var html = " ";
if (e.readyState == 4 && e.status == 200) {
response = JSON.parse(e.responseText);
if(typeof(e.responseText)==="string") {
d = e.responseText;
} else {
if (typeof(e.responseXML)==="object") {
d = e.responseXML;
};
}
var myData = response['JsonProjectIDResult'];
//Loop
var html = "<ul>";
var counter = 0;
for(var prop in myData[counter]) {
if(myData[counter].hasOwnProperty(prop))
html += "<li>" + prop + " = " + myData[counter][prop] + "</li>";
}
resultDiv.innerHTML = html;
}
};
e.open("GET", newsURL, true);
e.send();