【问题标题】:Geolocation and jQuery - Can't post results using ajax地理位置和 jQuery - 无法使用 ajax 发布结果
【发布时间】:2011-12-09 11:32:22
【问题描述】:

我目前正在做一个创建位置感知网站的项目。本质上,用户来到页面,使用 HTML5 方法找到他们的位置,然后使用 jQuery,该位置被发布到一个页面,该页面将位置/地址保存到 codeigniter 会话,但如果他们想更新他们的位置,或更改到不同的位置(即他们想使用他们的工作地址而不是他们现在的地址作为位置),有一个 jQuery 颜色框可以显示并让他们输入自定义地址。

一切都可以完美地获取初始位置,但是当我尝试保存更新的位置时,我收到错误“Uncaught TypeError: Object [object DOMWindow] has no method 'lat'”然后 Google Chrome 将其引用为不是 jQuery 中的错误,而是 Google Maps API 文件中的错误。有什么建议吗?

jQuery('.inputsubmit').click(function() {

//Takes values from user submitted fields and parses them into an address string
var street = jQuery('.inputaddress').val();
var city = jQuery('.inputcity').val();
var state = jQuery('.inputstate').val();


var address = street +" "+ city +", " +state;

   geocoder = new google.maps.Geocoder();
   var geocoderresult= geocoder.geocode({'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
           var newlocation = results[0].geometry.location;

               //Posts coordinates and address string to a CodeIgniter function to update users session information
               jQuery.post("somepage", {location: newlocation, address: address},function(data) {
             alert("Data Loaded: " + data);
           });

       } else {
          alert("Geocoder failed due to: " + status);
       }
    });

});

我已经尝试了我能想到的一切来让帖子正常工作。到目前为止,所有代码都可以正常工作,并且我已经注释掉了帖子行,并且一切正常。这是我们网站的主要功能之一,可根据位置提供即时快速的结果。

谢谢!

【问题讨论】:

  • 发布的javascript代码的哪一行导致了错误? Chrome 的检查员应该告诉你这个
  • 地图api文件main.js第12行
  • @matt b - 它发生在 Google Maps API 中。 “Google Chrome 引用的错误不是 jQuery,而是 Google Maps API 文件中的错误”。这通常意味着您向 API 传递了 API 不期望的参数。通过确认您传递给 API 的参数的值来调试它。
  • 是的,在我的代码中,我有一个警报框,通过函数的每个步骤显示每个值。 API 的所有内容都是用户输入的地址,它确实返回了正确的坐标,直到 jQuery.post 尝试运行它才会中断
  • “somepage”返回什么样的数据? “somepage”是否与当前页面在同一个域/子域/端口上?

标签: jquery ajax codeigniter google-maps-api-3 geolocation


【解决方案1】:

我认为除了原始页面之外,您还需要在 somepage 上加载您的 Google API 代码。因为$.POST 找不到lat 对象,所以somepage 中似乎没有包含geolocation.js 文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2010-10-17
    • 2015-05-17
    • 1970-01-01
    • 2013-07-20
    相关资源
    最近更新 更多