Parsererror 当将 AJAX 和 JSONP 与 Cordova 一起使用时



我正在尝试使用使用 Cordova 4.0.0 创建的移动应用程序将 4 个变量发布到外部服务器上的数据库中。

我已经看过无数与此类似的帖子,POST,GET,contentType等,但这些似乎都不起作用。我以前使用过 JSONP,它工作得很好(我使用该代码作为模板),但这似乎不起作用。

以下是控制台日志:

2015-01-30 09:19:09.817 Alert[1253:353531] {"readyState":4,"status":200,"statusText":"load"}
2015-01-30 09:19:09.817 Alert[1253:353531] parsererror
2015-01-30 09:19:09.817 Alert[1253:353531] {"line":2,"column":2097,"sourceURL":"file:///private/var/mobile/Containers/Bundle/Application/D1746CD9-C9B3-4A31-9965-E4A6AAED3347/Alert.app/www/js/jquery-2.1.3.min.js"}

这是我的 AJAX 帖子请求。

function pushToDataLog(beaconid, uuid, timestamp, status)
{
    jQuery.ajax({
        type: "POST", // HTTP method POST or GET
        url: "http://url.co.uk/ips/pushdata.php", //Where to make Ajax calls
        dataType:"jsonp", // Data type, HTML, json etc.
        jsonp: "callback",
        data : {beaconid: beaconid, uuid: uuid, timestamp: timestamp, status:status},
        crossDomain: true,
        contentType: 'application/json',
        success:function(response){
            //console.log(JSON.stringify(response));
        },
        error:function (xhr, ajaxOptions, thrownError){
            console.log(xhr);
            console.log(ajaxOptions);
            console.log(thrownError);
        }
    });
}

我不明白是什么导致了解析错误。

编辑

这是我存储在服务器上的PHP文件:

<?php
if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {

$beaconid = NULL;
$entertimestamp= NULL;
$exittimestamp = NULL;
//GET THE VARIABLES FROM JAVASCRIPT
if(isset($_REQUEST['beaconid'])){
    $beaconid = $_REQUEST['beaconid'];
}
if(isset($_REQUEST['uuid'])){
    $uuid = $_REQUEST['uuid'];
}
if(isset($_REQUEST['timestamp'])){
    $timestamp = $_REQUEST['timestamp'];
}
if(isset($_REQUEST['status'])){
    $status = $_REQUEST['status'];
}
define('DB_SERVER', 'a');
define('DB_USER', 'a');
define('DB_PASSWORD', 'a');
define('DB_DATABASE', 'a');
$mysqli = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE);
if (!$mysqli) {
    trigger_error('mysqli Connection failed! ' . htmlspecialchars(mysqli_connect_error()), E_USER_ERROR);
}else{
    print '<p>Database Connected!</p>';
}
//if( isset($name) && $name != '' ){
    $stmt = $mysqli->prepare("INSERT INTO a(BeaconID,UUID,Timestamp,Status) VALUES (?, ?, ?, ?)");
    if ($stmt === false) {
        trigger_error('Statement failed! ' . htmlspecialchars(mysqli_error($mysqli)), E_USER_ERROR);
    }
    $bind = mysqli_stmt_bind_param($stmt, "isii", $beaconid, $uuid, $timestamp, $status);
    if ($bind === false) {
        trigger_error('Bind param failed!', E_USER_ERROR);
    }
    $exec = mysqli_stmt_execute($stmt);
    if ($exec === false) {
        trigger_error('Statement execute failed! ' . htmlspecialchars(mysqli_stmt_error($stmt)), E_USER_ERROR); 
    }
    printf ("New Record has id %d.n", mysqli_insert_id($mysqli));
    //CLOSE THE SQL QUERY/STATEMENT
    mysqli_stmt_close($stmt);
    $json = json_encode("Success");
    echo $_GET['callback'].'('.$json.')';
//}
mysqli_close($mysqli);
}
?>

在@briosheje的帮助下,我设法解决了这个问题。如果将来有人遇到类似的问题,这是我解决它的方法:

问题 AJAX 请求很好,问题出在服务器上的 PHP 文件中。为了解决这个问题,我发回了一个包含成功/失败消息的 json 编码数组。

这是完整的 php 文件:

<?php
    $beaconid = NULL;
    $uuid = NULL;
    $timestamp = NULL;
    $status = NULL;
    //GET THE VARIABLES FROM JAVASCRIPT
    if(isset($_REQUEST['beaconid'])){
        $beaconid = $_REQUEST['beaconid'];
    }
    if(isset($_REQUEST['uuid'])){
        $uuid = $_REQUEST['uuid'];
    }
    if(isset($_REQUEST['timestamp'])){
        $timestamp = $_REQUEST['timestamp'];
    }
    if(isset($_REQUEST['status'])){
        $status = $_REQUEST['status'];
    }
    /*$beaconid = 1;
    $uuid = "1213sdfdsdf";
    $timestamp = 3424;
    $status = 1;*/

    define('DB_SERVER', 'xxx');
    define('DB_USER', 'xxx');
    define('DB_PASSWORD', 'x');
    define('DB_DATABASE', 'x');
    if($beaconid != '' && $uuid != '' && $timestamp != '' && $status != ''){
        $mysqli = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE);
        if (!$mysqli) {
            trigger_error('mysqli Connection failed! ' . htmlspecialchars(mysqli_connect_error()), E_USER_ERROR);
            $arr = array ('state'=>'connection failed');
        }else{
            $stmt = $mysqli->prepare("INSERT INTO datalog(BeaconID,UUID,Timestamp,Status) VALUES (?, ?, ?, ?)");
            $arr = array ('state'=>'data pushed to the datalog');
            if ($stmt === false) {
                trigger_error('Statement failed! ' . htmlspecialchars(mysqli_error($mysqli)), E_USER_ERROR);
                $arr = array ('state'=>'statement failed');
            }
            $bind = mysqli_stmt_bind_param($stmt, "isii", $beaconid, $uuid, $timestamp, $status);
            if ($bind === false) {
                trigger_error('Bind param failed!', E_USER_ERROR);
                $arr = array ('state'=>'build param failed');
            }
            $exec = mysqli_stmt_execute($stmt);
            if ($exec === false) {
                trigger_error('Statement execute failed! ' . htmlspecialchars(mysqli_stmt_error($stmt)), E_USER_ERROR); 
                $arr = array ('state'=>'statemente execute failed');
            }
        }
        $arr = json_encode($arr);   //response send to the app
        echo $_GET['callback'].'('.$arr.')';    //need this to prevent parsererror
        //CLOSE THE SQL QUERY/STATEMENT
        mysqli_stmt_close($stmt);

        mysqli_close($mysqli);
    }
?>

最新更新