【发布时间】: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