GDownloadUrl replacement in Gmap V3

前端 未结 1 1216
感动是毒
感动是毒 2021-01-29 00:57

I want to send and receive data from php files. What is the best possible way to do this?

What is the best replacement for GDownloadUrl in Google Map V3?

相关标签:
1条回答
  • 2021-01-29 01:46

    There is no equivalent to GDownloadUrl in the API V3. Loading data via AJAX is a general javascrip task that is not specific to the API or to Google Maps.

    Here's a function that will do the same:

    function ajaxLoad(url,callback,postData,plain) {
        var http_request = false;
    
        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType && plain) {
                http_request.overrideMimeType('text/plain');
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }
        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        http_request.onreadystatechange =  function() {
            if (http_request.readyState == 4) {
                if (http_request.status == 200) {
                    eval(callback(http_request));
                }
                else {
                    alert('Request Failed: ' + http_request.status);
                }
            }
        };
    
        if (postData) { // POST
            http_request.open('POST', url, true);
            http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');  
            http_request.setRequestHeader("Content-length", postData.length);
            http_request.send(postData);
        }
        else {
            http_request.open('GET', url, true);
            http_request.send(null);
        }
    }
    

    Make sure your server responds with a content-type:text/plain header

    Call it with postdsata:

    var postdata = 'a=1&b=2';
    ajaxLoad(serverUrl,myCallback,postdata);
    
    
    
    function myCallback(req){
    var txt = req.responseText;
    
    // optional, if needed to evaluate JSON
        eval(txt);
    }
    
    0 讨论(0)
提交回复
热议问题