【问题标题】:GDownloadUrl replacement in Gmap V3Gmap V3 中的 GDownloadUrl 替换
【发布时间】:2013-02-14 03:24:46
【问题描述】:

我想从 php 文件发送和接收数据。最好的方法是什么?

在 Google Map V3 中,GDownloadUrl 的最佳替代品是什么?

这是我现有的检查我是否插入成功的函数:

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

从 php 中获取数据的现有函数:

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

【问题讨论】:

    标签: php google-maps-api-3


    【解决方案1】:

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

    这里有一个函数可以做同样的事情:

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

    【讨论】:

    • 如果我想使用 post 发送数据来修改你的 ajax 代码?
    • 我将有许多变量和值要通过邮寄发送,但这里看起来我只有一个选项,或者我必须将它们全部放入一个字符串中,用 & 分隔?
    • @biz14,是的,您可以根据需要制作 postdata 字符串,始终以 & 分隔。否则,您可以尝试将其拆分为不同的调用。
    • @biz14 ,顺便说一句,如果你觉得有用,请考虑接受这个答案。
    • 我已经用一些代码更新了我的问题。我会接受你的回答,但我需要另外两个确认,一个是在我现有的 checkSaveGeoFenceData 我可以检查是否完全插入成功,所以在你的情况下,我必须通过 eval(callback(http_request)); 进行检查这会给我带来什么价值?接下来是我如何解析从 php 脚本接收到的数据,目前我通过 json 方法在 php 中使用此方法添加“array_push($data1, array( ...”
    猜你喜欢
    • 1970-01-01
    • 2012-07-17
    • 2011-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-10
    • 1970-01-01
    相关资源
    最近更新 更多