2011-07-08 23 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

什么是你的困惑? –