Jquery .Ajax - 如何传递数据

时间:2011-07-08 16:44:30

标签: php javascript jquery

我正在尝试通过jquery ajax调用传递变量。我不确定如何正确地做到这一点。我通过另一个html5脚本得到了lon lat坐标。

我如何获得另一侧的坐标?我试过$ _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,

仍然想要一个实际的解决方案。

3 个答案:

答案 0 :(得分:1)

这是我正在处理的项目中的一些代码。非常简单。

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

您可以使用.get切换.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);
});

答案 1 :(得分:0)

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

如果之前已定义,您可以使用location.coords.latitude

答案 2 :(得分: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();函数

的正确语法