2013-02-28 51 views
0

我想要發送和接收來自php文件的數據。什麼是最好的方式來做到這一點?Gmap V3中的GDownloadUrl替換

谷歌地圖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; 
} 

回答

1

沒有相當於的GDownloadUrl在API V3。通過AJAX加載數據是一項通用的javascrip任務,並非特定於API或Google Maps。

這裏有一個功能,將做同樣的:

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); 
} 
如果我要發送的數據
+0

使用後發什麼來修改你的ajax代碼? – biz14 2013-02-28 13:50:12

+0

@ biz14,我編輯了使用POST作爲選項的答案。 – Marcelo 2013-02-28 15:20:38

+0

我將有很多變量和值通過郵寄發送,但在這裏看起來我只有一個選項,或者我必須把它們全部放入一個字符串分隔符&? – biz14 2013-03-01 08:36:33