如何使用phonegap中的json从php检索数据?

时间:2014-09-16 10:14:47

标签: php jquery json cordova

这是我的示例php代码:

<?php
    $nai = 18;
    print json_encode($nai);
?>

我在www.example.com/reply.php添加了这些行。

我想在jquery中获取此值。这样我就可以使用该值来处理某些事情。

代码是否正确??

function connect() {
    alert("chek");
    var term = "nai";
    $.ajax({
        url: 'http://www.example.com/reply.php',
        type: 'POST',
        data: term,
        dataType: 'json',
        error: function(jqXHR, text_status, strError){
            alert(“no connection”);
        },
        timeout: 60000,
        success: function(data) {
            alert(data);
        }
    });
}

1 个答案:

答案 0 :(得分:1)

您需要设置PHP正在呈现的文件的标题。

默认情况下会呈现html,请在代码中使用它来解决此问题。

<?php
    header('Content-Type: application/json');
    $nai = 18;
    print json_encode($nai);
?>

现在这是phonegap的可读json格式。