好吧,我正在做一個網站,應該從數據庫中讀取標記,然後將它們填充到地圖上。如果我在本地工作,它工作正常,但是當我指向一個PHP文件是在線因此不同的域(從數據庫請求數據)我沒有得到任何響應,我有JSON.parse:意外的數據結束。請注意,我不想更改php文件中的任何內容,因爲另一個網站已經在使用此文件。正在調用和執行請求的函數如下所示...非常感謝您的幫助。跨多個域的json響應
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);
}
檢查[這個答案](http://stackoverflow.com/a/3771928/1229023)的一些提示。順便問一下類似的問題,很多時候,順便說一句。 – raina77ow
可能的重複[方法來規避同源政策](http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy) – Quentin
我認爲這是一個很好的答案。 .. http://stackoverflow.com/questions/3506208/jquery-ajax-cross-domain – ramsinb