Javascript 使用AJAX将地理位置数据发布到PHP
我看过一些类似的线索,但看不出我到底出了什么问题。 我正在使用AJAX和PHP在我正在创建的应用程序上保存用户的lat和lng。我知道AJAX和PHP可以工作,因为它将所有内容(甚至虚拟lat和lng值)保存到我的数据库表中。 我已经玩了几个小时的变量,到目前为止,我得到的最好结果是在数据库中插入一个“0”值Javascript 使用AJAX将地理位置数据发布到PHP,javascript,ajax,cordova,geolocation,Javascript,Ajax,Cordova,Geolocation,我看过一些类似的线索,但看不出我到底出了什么问题。 我正在使用AJAX和PHP在我正在创建的应用程序上保存用户的lat和lng。我知道AJAX和PHP可以工作,因为它将所有内容(甚至虚拟lat和lng值)保存到我的数据库表中。 我已经玩了几个小时的变量,到目前为止,我得到的最好结果是在数据库中插入一个“0”值 document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { get
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函数
任何帮助都将不胜感激。:)
劳拉也许我们看不到所有代码,但看起来纬度和经度是定位成功的函数范围。因此,当您尝试在ajaxFunction中访问它们时,它们将获得默认值 此外,您不需要做所有的getXMLObject有趣的事情。在Android、iOS和BlackBerry等webkit浏览器上,您只需执行以下操作:
var xmlhttp = new XMLHttpRequest();
最后,由于您可能正在运行文件://协议,因此必须查找状态代码0,在本例中类似于200
function handleServerResponse() {
if (xmlhttp.readyState == 4) {
if(xmlhttp.status == 200 || xmlhttp.status == 0) {
document.getElementById("message").innerHTML=xmlhttp.responseText;
}
}
// etc.
}
为什么不直接使用jQueryAjax调用呢?您的代码已标记为IE6。。。由于这是在phonegap中标记的,我假设您可以使用更新的方法来做事情 我建议使用jQueryAjax
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);
}
});
}
您是否检查了PHP端接收到的数据?因为那看起来应该没问题。如果使用INT保存纬度和经度hi,问题可能在数据库端,它是VARCHAR-刚刚检查过,但感谢您的帮助。:)