Can I use PhoneGap jQuery to make AJAX calls?
Can I use jQuery AJAX calls in PhoneGap to run a PHP fi开发者_StackOverflow社区le that gets records from a database, or should I use JavaScript AAJAX?
You certainly can use jQuery Ajax functions in your PhoneGap applications. Here is a demo:
-- JavaScript in App --
$('#some_page_id').bind('pageshow', function () {
$.get('http://domain.com/path/to/script.php?get_param=value', function (data) {
$(this).find('div[data-role="content"]').append(data);
});
});
-- PHP on Server --
if (isset($_GET['get_param']) && $_GET['get_param'] == 'value') {
$query = mysql_query("SELECT * FROM some_table WHERE some_col='something'", $db_handle);
if (mysql_affected_rows() > 0) {
while ($row = mysql_fetch_assoc($query)) {
echo "<div>" . $row['some_other_col'] . "</div>";
}
} else {
echo "No Data Found";
}
}
The above example will query the PHP script on the server every time the '#some_page_id' page is shown and append the data grabbed to the <div data-role="content">
tag. You can also use .html(data)
instead of .append(data)
to replace the HTML rather than add to it.
UPDATE
I found this in the jQuery Mobile documentation which gives some excellent information about making $.ajax()
calls in PhoneGap apps: http://jquerymobile.com/demos/1.0/docs/pages/phonegap.html
精彩评论