【问题标题】:Posting Geolocation data to PHP using AJAX使用 AJAX 将地理位置数据发布到 PHP
【发布时间】:2012-01-25 15:24:13
【问题描述】:

我查看了一些类似的线程 - 但无法确切地看到我哪里出错了。 我正在使用 AJAX 和 PHP 在我正在创建的应用程序上保存用户的纬度和经度。我知道 AJAX 和 PHP 可以工作,因为它将所有内容(甚至是虚拟 lat 和 lng 值)保存到我的数据库表中。 我已经玩了几个小时的变量了,到目前为止我得到的最好结果是在数据库中插入了一个 '0' 值。

document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
getCurrentLocation();
}
function onError(message) {
navigator.notification.alert(message, "", "Error");
}
function getCurrentLocation() {
navigator.geolocation.getCurrentPosition(locationSuccess, onError);
}
function locationSuccess(position) {
lat = document.getElementById("latSpan");
lon = document.getElementById("latSpan");
latitude = position.coords.latitude;
longitude = position.coords.longitude;
}
//recording function
function getXMLObject()  //XML OBJECT
{
var xmlHttp = false;
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
}
catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
}
catch (e2) {
xmlHttp = false   // No Browser accepts the XMLHTTP Object then false
}
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') 
{ 
xmlHttp = new XMLHttpRequest();        //For Mozilla, Opera Browsers
}
return xmlHttp;  // Mandatory Statement returning the ajax object created
}
var xmlhttp = new getXMLObject();   //xmlhttp holds the ajax object 
function ajaxFunction() {
var getdate = new Date();  //Used to prevent caching during ajax call
if(xmlhttp) { 
xmlhttp.open("POST","http://www.lauracrane.co.uk/app/rec/location.php",true); //
xmlhttp.onreadystatechange  = handleServerResponse;
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send("latitude=" + latitude + "&longitude=" + longitude);
}
}
function handleServerResponse() {
if (xmlhttp.readyState == 4) {
if(xmlhttp.status == 200) {
    document.getElementById("message").innerHTML=xmlhttp.responseText;
}
else {
alert("Error during AJAX call. Please try again");
}
}}'

我想知道是否有人可以看到为什么它没有将 lat & lng 的值传递给 AJAX 函数。

任何帮助将不胜感激。 :)

劳拉

【问题讨论】:

  • 你检查了PHP端收到的数据吗?因为那看起来应该没问题。如果您使用 INT 保存纬度和经度,问题可能出在数据库方面
  • 嗨,它是 VARCHAR - 刚刚检查过,但感谢您的帮助。 :)

标签: javascript ajax cordova geolocation


【解决方案1】:

也许我们看不到所有代码,但看起来纬度和经度是作用域为 locationSuccess 的函数。因此,当您尝试在 ajaxFunction 中访问它们时,它们将获得默认值。

此外,您不需要做所有 getXMLObject 的乐趣。在 Android、iOS 和 BlackBerry 等 webkit 浏览器上,您只需执行以下操作:

var xmlhttp = new XMLHttpRequest();

最后,由于您可能在 file:// 协议之外运行它,因此您必须查找状态代码 0,在这种情况下类似于 200。

function handleServerResponse() {
    if (xmlhttp.readyState == 4) {
        if(xmlhttp.status == 200 || xmlhttp.status == 0) {
            document.getElementById("message").innerHTML=xmlhttp.responseText;
        }
    }
    // etc.
}

【讨论】:

    【解决方案2】:

    为什么不直接使用 jQuery ajax 调用呢?您的代码已针对 IE6 进行了标记...由于在 phonegap 中标记了此代码,因此我假设您可以使用更新的处理方法。

    我建议使用 jQuery ajax..

    function ajaxFunction() {
    $.ajax({
      url:'http://www.lauracrane.co.uk/app/rec/location.php',
      type:'POST',
      data:'lat='+lat+'&long='+long,
      success:function(d){
        console.log(d);
      },
      error(w,t,f){
        console.log(w+' '+t+' '+f);
      }
    });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-09
      • 1970-01-01
      • 2011-12-09
      • 1970-01-01
      相关资源
      最近更新 更多