如何从phonegap android中的php解码编码的json数据

时间:2014-06-10 08:56:58

标签: php android jquery ajax json

我想将表数据从服务器存储到android本地存储。但问题是表数据可能包含像'<'这样的特殊字符或'>'两者都是。因此,当进行ajax调用时,我会在从服务器插入数据时获得sqllite错误代码13。如果我尝试从服务器存储简单到localStorage它完美地工作。 所以我试图在php中编码数据,然后以json格式将数据发送到Android应用程序。但是知道它在ajax数据中得到空白数据。 以下是编码数据的PHP代码:

$select_table = 'select * from users';
$resultTableData = $conn->query($select_table);
while ($row1 = $resultTableData->fetch()) {
    $temporarydata[] = htmlentities(utf8_encode($row1),ENT_QUOTES);
}   
$data[$row] = $temporarydata;
mysql_close($con);
require_once('JSON.php');
$json = new Services_JSON();
echo ($json->encode($data));

以下是PhoneGap Android中的ajax代码:

$.ajax({
    url: urlServer + 'getDynamicFormData.php',
    contentType: 'application/json',
    beforeSend: function() {
        $.mobile.loading('show');
    },
    complete: function() {
        console.log("ajax complete");
        createTable();
    },
    dataType: 'json',
    data: {
        companyCode : companycode
    },
    success: function(data) {
        console.log('value in data: '+JSON.stringify(data));
        if (data != null) {
            dynamic_tabledetails = data.Table_details;
            table_data = data;
            getTabledetails(dynamic_tabledetails);                  
        } else {
            alert("Error Message");
        }
    },
    error: function(xhr, ajaxOptions, thrownError) {
        console.log("Server is not responding... Please try again: "+thrownError);
    }
});

1 个答案:

答案 0 :(得分:0)

如果您收到JSON答案,请尝试查看GSON,这可能会对您有所帮助。