简体   繁体   English

使用AJAX将地理位置数据发布到PHP

[英]Posting Geolocation data to PHP using AJAX

I've looked through a few similar threads - but can't see exactly where I am going wrong. 我已经浏览了一些类似的主题-但无法确切看到我要去哪里。 I'm savng the lat & lng of a user on an app I'm creating, using AJAX & PHP. 我使用AJAX和PHP在正在创建的应用程序上保存用户的经度。 I know the AJAX & PHP works as it saves everything (even dummy lat & lng values) to my database table. 我知道AJAX和PHP可以正常工作,因为它将所有内容(甚至是虚拟的lat和lng值)保存到我的数据库表中。 I've been playing with the variables for a couple of hours now, and the best results I have had so far is a '0' value inserted in the database. 我已经使用变量两个小时了,到目前为止,我获得的最好结果是在数据库中插入了“ 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");
}
}}'

I was wondering if someone can see why it's not passing the value of lat & lng to the AJAX function. 我想知道是否有人可以看到为什么不将lat&lng的值传递给AJAX函数。

Any help would be appreciated. 任何帮助,将不胜感激。 :) :)

Laura 劳拉

Maybe we can't see all the code but it looks like latitude and longitude are function scoped to locationSuccess. 也许我们看不到所有代码,但是看起来纬度和经度的功能范围仅限于locationSuccess。 So when you try to access them in ajaxFunction they will get the default values. 因此,当您尝试在ajaxFunction中访问它们时,它们将获得默认值。

Also, you don't need to do all that getXMLObject fun. 而且,您不需要做所有有趣的getXMLObject。 On webkit browsers like in Android, iOS and BlackBerry you just need to do: 在Android,iOS和BlackBerry等Webkit浏览器中,您只需要执行以下操作:

var xmlhttp = new XMLHttpRequest();

And finally since you are probably running this off of the file:// protocol you will have to look for a status code of 0 which is analogous to 200 in this case. 最后,由于您可能是在file://协议之外运行的,因此您必须查找状态码0,在这种情况下,该状态码类似于200。

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

Why not just use a jQuery ajax call? 为什么不只使用jQuery ajax调用呢? Your code is marked up for IE6... Since this in tagged in phonegap I would assume you have access to using newer methods of doing things. 您的代码已针对IE6进行了标记...由于在phonegap中已对此进行了标记,因此我认为您可以使用更新的方法进行访问。

I would suggest using jQuery ajax.. 我建议使用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);
  }
});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM