<script type="text/javascript">
function passCheck(){
var pass1 = document.getElementById('password').value;
var pass2 = document.getElementById('confirmPassword').value;
if(pass1 == pass2 && pass1 != ""){
return true;
}
else{
alert("Both password inputs do not match. Please retry.");
document.getElementById('surveyorForm').reset();
return false;
}
}
function change() {
var x = document.getElementById("select").value;
(function () {
var xmlhttp = new XMLHttpRequest();
var url = "http://somejsondata?someget=" + x ;
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
myFunction(xmlhttp.responseText);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
function myFunction(response) {
var arr = JSON.parse(response);
var i;
var out = "<div class='form-group'>" +
"<label>City</label>" +
"<select class='form-control' name='cityId'>" +
"<option disabled selected>Select your option</option>";
for(i = 0; i < arr.length; i++) {
out += "<option value='" +
arr[i].cityId +
"'>" +
arr[i].cityName +
"</option>";
}
out += "</select>" +
"</div>";
document.getElementById("city").innerHTML = out;
}
})();
}
(function () {
var xmlhttp = new XMLHttpRequest();
var url = "http://somejsondata";
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
myFunction(xmlhttp.responseText);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
function myFunction(response) {
var arr = JSON.parse(response);
var i;
var out = "<div class='form-group'>" +
"<label>Province</label>" +
"<select class='form-control' id='select' name='provId' onchange='change()'>" +
"<option disabled selected>Select your option</option>";
for(i = 0; i < arr.length; i++) {
out += "<option value='" +
arr[i].provinceId +
"'>" +
arr[i].provinceName +
"</option>";
}
out += "</select>" +
"</div>";
document.getElementById("province").innerHTML = out;
}
})();
(function () {
var xmlhttp = new XMLHttpRequest();
var url = "http://somejsondata";
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
myFunction(xmlhttp.responseText);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
function myFunction(response) {
var arr = JSON.parse(response);
var i;
var out = "<div class='form-group'>" +
"<label>Country</label>" +
"<select class='form-control' name='countryId'>" +
"<option disabled selected>Select your option</option>";
for(i = 0; i < arr.length; i++) {
out += "<option value='" +
arr[i].countryId +
"'>" +
arr[i].countryName +
"</option>";
}
out += "</select>" +
"</div>";
document.getElementById("country").innerHTML = out;
}
})();
</script>
我已将此脚本放在</body>
之前,并检查其工作的链接,从而生成json数据。但是,当我访问网站时,为什么在迁移网站之前它并没有像我一样出现。下拉菜单没有显示出来。
我添加了一些错误 这是什么样的错误
XMLHttpRequest cannot load http://gpx1.gdn/country/read. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://gpx2.gdn' is therefore not allowed access.
insertSurveyor:1 XMLHttpRequest cannot load http://gpx1.gdn/province/read. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://gpx2.gdn' is therefore not allowed access.
firebug-lite.js:11883 Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.fetchResource @ firebug-lite.js:11883
firebug-lite.js:30905 Uncaught TypeError: Cannot read property 'push' of undefined(…)
答案 0 :(得分:1)
如果我是对的,你正在对另一个域进行XMLHttpRequest。因此浏览器会阻止它,因为出于安全原因,它通常允许同一来源的请求。当您想要执行跨域请求时,您需要做一些不同的事情。
跨源资源共享(CORS)是一种允许来自浏览器的跨域通信的W3C规范。通过构建在XMLHttpRequest对象之上,CORS允许开发人员使用与同域请求相同的习语。
您可以随时查看此post以了解更多信息。下面给出了CORS样本。
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// Check if the XMLHttpRequest object has a "withCredentials" property.
// "withCredentials" only exists on XMLHTTPRequest2 objects.
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
// Otherwise, check if XDomainRequest.
// XDomainRequest only exists in IE, and is IE's way of making CORS requests.
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
// Otherwise, CORS is not supported by the browser.
xhr = null;
}
return xhr;
}
var xhr = createCORSRequest('GET', url);
if (!xhr) {
throw new Error('CORS not supported');
}
这link非常好地解释了CORS。