简体   繁体   中英

json response across multiple domains

well i am doing a website which should read markers from a db and then populate them on the map. If i work locally it works fine but when i point to a php file is online thus different domain ( which requests data from the db ) i am not getting any response and i am having JSON.parse: unexpected end of data. Note i dont want to change anything in the php file because another website is already using this file. the function which is calling and doing the request is shown below... your help is much appreciated.

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);
}

Ajax queries traditionally need to be sent to the same server because of the same origin policy .

You can allow connection from other site by adding

<?php header("Access-Control-Allow-Origin: *"); ?> 

to you PHP script.

An alternative would be to use JSONP

You have to use cross-domain techniqes such as JSONP. Browsers does not allow accessing servers in a different domain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM