私がやろうとしているのは、オンライン Web サービスから JSON データを取得して、MySQL データベース内の特定の文字列を検索して見つけ、Javascript を使用して HTML ページに表示することです。
私が苦労しているのは、実際に結果のデータを表示することです。
私の HTML ページの関連する領域は次のようになります。
<form onSubmit="results()">
<fieldset>
<label for="area">First digits of postal code:</label>
<input name="area" type="text" maxlength="4" placeholder="AB12" required />
<input type="submit" value="Search" name="search" />
</fieldset>
</form>
<script type="text/javascript" src="jquery/jquery.min.js"></script>
<script type="text/javascript" src="cordova-2.3.0.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
function results(){
$.ajax({
url: 'http://www.foobar.com/cp/index.php?area=AB12',
dataType: 'jsonp',
jsonp: 'jsoncallback',
timeout: 5000,
success: function(data, status){
$.each(data, function(i,item){
var place = '<h1>'+item.location+'</h1>'
+ '<p>'+item.id+'</br>';
output.append(place);
});
},
error: function(){
output.text('There was an error loading the data.');
}
});
};
</script>
<div id="place">
<h3>Places near your location</h3>
</div>
GET データのページはhttp://www.foobar.com/cp/index.phpで、検索変数は「area」です。テストサンプルは ?area=AB12です。