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

时间:2015-05-12 13:31:32

标签: php ios ajax

我创建了一个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);
    }
});

这是来自handler.php文件的代码,ajax请求请求。

$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。有人知道吗?

1 个答案:

答案 0 :(得分:1)

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