2011-07-08 20 views
2

我想通過jquery ajax調用傳遞一個變量。我不確定如何正確地做到這一點。我通過另一個html5腳本獲得最大緯度座標。Jquery .Ajax - 如何傳遞數據

如何獲得另一側的座標?我試過$ _GET(lat)。

我也不確定我是否可以在不同的<腳本中使用location.coords.latitude>。

$.ajax({ 
    cache: false, 
    url: "mobile/nearby.php", 
    dataType: "html", 
    data: "lat="+location.coords.latitude+"&lon="+loc.coords.longitude+, 
    success: function (data2) { 
    $("#nearbysgeo").html(data2); 

    } 
}); 

這些腳本是jQuery代碼上面

<script type="text/javascript"> 
    google.setOnLoadCallback(function() { 

    $(function() { 

     navigator.geolocation.getCurrentPosition(displayCoordinates); 

     function displayCoordinates(location) { 

     var map = new GMap2(document.getElementById("location")); 
     map.setCenter(new GLatLng(location.coords.latitude, location.coords.longitude), 12); 
     map.setUIToDefault(); 
     var point = new GLatLng(location.coords.latitude, location.coords.longitude); 
     var marker = new GMarker(point); 
     map.addOverlay(marker); 


     } 

    }) 
    }); 
</script> 
    <script type="text/javascript" charset="utf-8"> 

    function getLocation(){ 

     if (navigator.geolocation) { 

      navigator.geolocation.getCurrentPosition(success, error); 

     } else { 

      document.getElementById("output").innerHTML = "Your browser doesn't handle the GeoLocation API. Use Safari, Firefox 4 or Chrome"; 

     } 


    } 
    function success(loc){ 

     console.log(loc); 

     strout = ""; 

     for(l in loc.coords){ 

      //strout += l +" = " +loc.coords[l] + "<br>"; 

     } 
     strout += ''; 
     strout += '<center><img src="http://maps.google.com/maps/api/staticmap?center='+loc.coords.latitude+','+loc.coords.longitude+'&markers=color:blue%7Clabel:Y%7C'+loc.coords.latitude+','+ loc.coords.longitude+'&zoom=15&size=400x250&sensor=false&center=currentPosition"></center>'; 

     document.getElementById("output").innerHTML = strout; 

     document.forms['newPostForm'].lat.value = loc.coords.latitude; 
     document.forms['newPostForm'].lon.value = loc.coords.longitude; 
     document.getElementById("coords").innerHTML = ''; 
     document.getElementById("coords").innerHTML = 'CURRENT: Lat:' + loc.coords.latitude + ' Lon:' + loc.coords.longitude; 

    } 


    function error(err){ 

     document.getElementById("output").innerHTML = err.message; 

    } 

    function clearBlog() { 
      document.getElementById("listview").innerHTML = ''; 
     } 
    </script> 

附加信息:

它的工作原理,如果我用這條線。所以我想我不能用這種方式使用loc.coords.latitude。

data: "&lat=43&lon=-79.3", 

那麼我現在就砍掉它,讓它工作。我使用lon和lat值在頁面上填充了兩個隱藏的表單元素。然後使用'document.forms ['newPostForm']。lat.value'創建一個像這樣的線。

data: "&lat="+document.forms['newPostForm'].lat.value+"&lon="+document.forms['newPostForm'].lon.value, 

還想要一個實際的解決方案。

+0

的正確語法你能展現其中'location'(或'loc')定義的例子嗎? –

回答

1

下面是我正在處理的項目中的一些代碼。很簡單。

$.post("../postHandler.php", { post_action: "getRecentPosts", limit: "10" }, function(data){ 
      $("#post-list").html(data); 

您可以用.post的用。獲得沒有其他變化切換出來,像這樣:

$.get("../postHandler.php", { post_action: "getRecentPosts", limit: "10" }, function(data){ 
      $("#post-list").html(data); 

數據的名稱值對傳遞像這樣。

{ post_action: "getRecentPosts", limit: "10" } 

重寫:

$.get("mobile/nearby.php", { lat: location.coords.latitude, lon: loc.coords.longitude }, function(data2){ 
      $("#nearbysgeo").html(data2); 
}); 
+0

我使用座標來返回html。我能用.get做到這一點嗎? – vitalyp

+0

我試過.get,但它似乎不工作。我不回HTML。我沒有忘記右括號 – vitalyp

+0

是的,你將能夠返回html。 – bitbandit

0
$lat = preg_replace('#[^0-9\.]#', '', $_GET['lat']); 

如果以前定義過,您可能可以使用location.coords.latitude

+0

我不知道它之前是否定義過。你的意思是'var location.coords.latitude'在什麼地方?不,我沒有那個。 – vitalyp

0
jQuery.ajax(
       { 
        url  : 'mobile/nearby.php', 
        data : { 
            'action'  : 'update', 
            'newname' : 'enteredText', 
            'oldname' : 'original_html', 
            'userid'  : '10' 
           }, 
        success : function(msg){ 
            if(msg == 1) 
            { 
              alert('success'); 
            } 
           } 
       }); 

這是jQuery.Ajax();功能

+0

這使我更困惑 – vitalyp

+0

什麼是你的困惑? –