gpt4 book ai didi

php - Ajax 调用不适用于 iPhone 应用程序和 arduino

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:15:36 27 4
gpt4 key购买 nike

我创建了一个 Arduino 项目,用于将坐标发送到 URL。该 URL 执行一些 ajax 调用。在浏览器中它工作正常,但是当我用 Arduino 尝试它时它不起作用。所以我尝试用 iOS 应用程序做同样的事情,但我遇到了同样的问题。这是 Arduino 和 iOS 应用程序请求的页面上的代码。

var directionsService = new google.maps.DirectionsService();
var base_url = window.location;
var received_data = <?php echo json_encode($received_data); ?>;

$.ajax({
url: 'http://gps-tracker.domain.nl/_api/handler.php',
data: { action: 'post', device_id: received_data['device_id']},
type: 'GET',
dataType:"jsonp",
jsonp:"callback",
success: function (response){
var error = [];
var total = response.length;

for (var type in response) {

if(response[type].types == 'area'){

var x = checkInsideCircle(response[type].longitude, response[type].latitude, received_data['longitude'], received_data['latitude'], response[type].reach / 1000);

if(x == false){
// Outside
error.push(true);
}else{
// Inside
error.push(false);
}
}else if(response[type].types == 'route'){

// Check route
checkOnRoute(response[type].start_latitude, response[type].start_longitude, response[type].end_latitude, response[type].end_longitude, response[type].type, response[type]['reach'], type, function(result) {

error.push(result);

if(error.length == total){

if(error.indexOf(false) >= 0){

// Device is inside route or area
outside = false;
}else{
// Send data to database
$.ajax({
url: 'http://gps-tracker.domain.nl/_api/handler.php',
data: { action: 'post', device_id: received_data['device_id'], longitude: received_data['longitude'], latitude: received_data['latitude']},
type: 'GET',
dataType: 'json',
success: function (response){
console.log('good');
},error: function(jq,status,message) {
alert('A jQuery error has occurred. Status: ' + status + ' - Message: ' + message);
}
});
}
}
});
}
}
},error: function(jq,status,message) {
alert('A jQuery error has occurred. Status: ' + status + ' - Message: ' + message);
}
});

这是 ajax 请求请求的 handler.php 文件中的代码。

$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : false;

// Switch actions
switch($action) {
case 'get':
$callback ='callback';

if(isset($_GET['callback'])){
$callback = $_GET['callback'];
}

$routes = ORM::for_table('gps_tracker_route')
->inner_join('gps_tracker_device', array('gps_tracker_device.device_id', '=', 'gps_tracker_route.device_id'))
->where('gps_tracker_route.device_id', $_GET['device_id'])
->where('gps_tracker_device.device_id', $_GET['device_id']);

if($routes = $routes->find_many()){
foreach($routes as $k=>$v){
$v = $v->as_array();

if($v['status'] == 'on' or strtotime(date('Y-m-d H:i:s')) > strtotime($v['start_time']) and strtotime(date('Y-m-d H:i:s')) < strtotime($v['end_time'])){
$response1[$k] = $v;
$response1[$k]['types'] = 'route';
}
}
}

$area = ORM::for_table('gps_tracker_area')
->inner_join('gps_tracker_device', array('gps_tracker_device.device_id', '=', 'gps_tracker_area.device_id'))
->where('gps_tracker_area.device_id', $_GET['device_id'])
->where('gps_tracker_device.device_id', $_GET['device_id']);

if($area = $area->find_many()){
foreach($area as $k=>$v){
$v = $v->as_array();

if($v['status'] == 'on' or strtotime(date('Y-m-d H:i:s')) > strtotime($v['start_time']) and strtotime(date('Y-m-d H:i:s')) < strtotime($v['end_time'])){
$response2[$k] = $v;
$response2[$k]['types'] = 'area';
}
}
}

if(isset($response1) and isset($response2)){
$response = array_merge($response1, $response2);
}elseif(isset($response1)){
$response = $response1;
}else{
$response = $response2;
}

if ( isset($response) ) {

if ( is_array($response) ) {
if (function_exists('json_encode')) {
header('Content-Type: application/json');

echo $callback.'(' . json_encode($response) . ')';
} else {
include( ABSOLUTE_PATH . '/classes/json.class.php');
$json = new Services_JSON();
echo $json->encode($response);
}
} else {
echo $response;
}
exit(0);
}else{
exit();
}
break;
case 'post':

$_GET['timestamp'] = date("Y-m-d H:i:s");

$record = ORM::for_table('gps_tracker_device_logging')->create($_GET);
$record->save();

$item = ORM::for_table('gps_tracker_device_logging')
->where('id', $record->id);


if($item = $item->find_one()){

$item = $item->as_array();

echo json_encode($item);
}

break;
default:
die('invalid call');
}

有人可以帮助我吗?

编辑

我认为它与 Javascript 有关。我不知道当像 Arduino 这样的设备向服务器发出 http 请求时是否可以使用 javascript。有人知道吗?

最佳答案

我认为这是因为您需要一个支持 JavaScript 的 Web 浏览器。我不使用 Arduino,但据我所知,它没有“真正的”网络浏览器——它只能拉/下载数据,但不能执行 JS 部分。为了让 JS 工作,你需要一些东西来运行它。这就是它在浏览器中工作的原因。

关于php - Ajax 调用不适用于 iPhone 应用程序和 arduino,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30192432/

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