gpt4 book ai didi

javascript - 如何从 JavaScript 获取数据到 Controller 并将其存储在数据库中

转载 作者:行者123 更新时间:2023-12-01 00:36:24 25 4
gpt4 key购买 nike

我想获取从此 JavaScript 到 Controller 的位置并将其存储在数据库中:

var currPosition;

navigator.geolocation.getCurrentPosition(function(position) {
updatePosition(position);
setInterval(function() {
var lat = currPosition.coords.latitude;
var lng = currPosition.coords.longitude;
jQuery.ajax({
type: "POST",
url: "myURL/location.php",
data: 'x=' + lat + '&y=' + lng,
cache: false
});
}, 1000);
}, errorCallback);

var watchID = navigator.geolocation.watchPosition(function(position) {
updatePosition(position);
});

function updatePosition(position) {
currPosition = position;
}

function errorCallback(error) {
var msg = "Can't get your location. Error = ";
if (error.code == 1)
msg += "PERMISSION_DENIED";
else if (error.code == 2)
msg += "POSITION_UNAVAILABLE";
else if (error.code == 3)
msg += "TIMEOUT";
msg += ", msg = " + error.message;
alert(msg);
}

最佳答案

使用 .ajax() 发送帖子:

// ....
let formData = new FormData();
const lat = currPosition.coords.latitude;
const lng = currPosition.coords.longitude;
formData.append("x", lat);
formData.append("y", y lng;
$.ajax({
url: "myURL/location.php", // update this with you url
dataType: 'text',
cache: false,
contentType: false,
processData: false,
data: formData,
type: 'POST',
success: function(data){
const response = jQuery.parseJSON(data);
}
});
//....

接收codeigniter中的发布数据:

$x = $this->input->post('x');
$y = $this->input->post('y');

关于javascript - 如何从 JavaScript 获取数据到 Controller 并将其存储在数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58099351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com