0

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?

Here is my existing function to check if I inserted successfully:

function checkSaveGeoFenceData(data,code)
{
    //var geoFenceDataJson = eval('(' + json + ')'); 

    if(code===200)
    {
        //alert("JSON VALUE : "+data+"test");
        if(data=="SMGFE\n")
        {
            alert("Geo Fence " + 
                document.getElementById("geoFenceName").value + 
                " Already Exist");
        }
        else {
            alert("Successfully Inserted");

            window.opener.location.reload();
            window.close();
        }
    }
    else
    {
        alert("Fail to insert");
    }
}

Existing function to grab data from php:

function processtViewData(json)
{
    dataJson = eval('(' + json + ')'); 
    var totalData = dataJson.data1.length;
}
4

1 回答 1

1

API V3 中没有与 GDownloadUrl 等效的功能。通过 AJAX 加载数据是一个通用的 javascrip 任务,它并不特定于 API 或谷歌地图。

这是一个可以执行相同操作的函数:

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

确保您的服务器以content-type:text/plain标头响应

用 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);
}
于 2013-02-28T07:57:56.473 回答