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


How to retrieve data from php using json in phonegap?

这是我的示例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);
        }
    });
}

您需要设置PHP正在渲染的文件的头。

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

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

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