[英]json response across multiple domains
好吧,我正在做一个网站,该网站应该从数据库读取标记,然后在地图上填充它们。 如果我在本地工作,则可以正常工作,但是当我指向一个在线的php文件时,则不同的域(从db请求数据)我没有任何响应,并且我有JSON.parse:数据意外结束。 注意,我不想更改php文件中的任何内容,因为另一个网站已经在使用此文件。 下面显示了正在调用和执行请求的函数...非常感谢您的帮助。
function ajaxrequestDB() {
var AJAX = null; // Initialize the AJAX variable.
if (window.XMLHttpRequest) { // Does this browser have an XMLHttpRequest object?
AJAX=new XMLHttpRequest(); // Yes -- initialize it.
}
else { // No, try to initialize it IE style
AJAX=new ActiveXObject("Microsoft.XMLHTTP"); // Wheee, ActiveX, how do we format c: again?
} // End setup Ajax.
if (AJAX==null){ // If we couldn't initialize Ajax...
alert("Your browser doesn't support AJAX."); // Sorry msg.
return false // Return false, couldn't set up ajax
}
AJAX.onreadystatechange = function() { // When the browser has the request info..
if (AJAX.readyState==4 || AJAX.readyState=="complete")
{ // see if the complete flag is set.
//alert(AJAX.responseText);
var result =JSON.parse(AJAX.responseText);
//alert(AJAX.responseText);
for (var i=0; i<result.length; i++) {
for (var j=0; j<gmarkers.length; j++) {
if (gmarkers[j].myname == result[i].name) {
gmarkers[j].setVisible(true);
gcircle[j].bindTo('center', gmarkers[j], 'position');
gcircle[j].setVisible(true);
var cat = gmarkers[j].mycategory;
}
}
}
callback(AJAX.responseText, AJAX.status); // Pass the response to our processing function
} // End Ajax readystate check.
}
//var url='http://localhost/refresh.php'; //this works !
var url='http://anotherdomain.org/Scripts/refresh.php';
AJAX.open("GET", url, true); // Open the url this object was set-up with.
AJAX.send(); // Send the request.
}
function callback(x, y) {
// alert(x);
}
您必须使用跨域技术,例如JSONP。 浏览器不允许访问其他域中的服务器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.