Here is my code. I keep getting a json parser error. The ajax never goes into done. Help!
<input type="submit" class="button" name="insert" value="load"/>
<div id="wines">
<!-- Javascript will print data in here when we have finished the page -->
</div>
jQuery(document).ready(function() {
var $ = jQuery;
var baseUrl = [location.protocol, '//', location.host, location.pathname].join('');
$('.button').click(function(){ // This event fires when a button is clicked
var button = $(this).val();
$.ajax({ // ajax call starts
url: baseUrl, // JQuery loads serverside.php
data: 'action=' + $(this).val(), // Send value of the clicked button
dataType: 'json', // Choosing a JSON datatype
}).done(function(data) { // Variable data contains the data we get from serverside
console.log("j");
}).fail(function(data,error) {
console.log(error);
})
});
});
<?php
$action = req('action');
// Red wine table
$red = array('Chianti', 'Barolo', 'Pinot Noir');
$white = array('Chardonnay', 'Cava', 'Chablis');
// Combine red and white tables into one multidimensional table
$winetable = array(
"red" => $red,
"white" => $white,
);
// Finally depending on the button value, JSON encode our winetable and print it
if ($action == "load") {
print json_encode($red);
header('Content-Type: application/json');
}
?>
UPDATE: Error message shows this in the console:
"Initializing System Events for WUH..." common_admin.js:22
"["Chianti","Barolo","Pinot Noir"]
<input type="submit" class="button" name="insert" value="load"/>
<div id="wines">
<!-- Javascript will print data in here when we have finished the page -->
</div>
<script type="text/javascript">
jQuery(document).ready(function() {
var $ = jQuery;
var baseUrl = [location.protocol, '//', location.host, location.pathname].join('');
$('.button').click(function(){ // This event fires when a button is clicked
var button = $(this).val();
$.ajax({ // ajax call starts
url: baseUrl, // JQuery loads serverside.php
data: 'action=' + $(this).val(), // Send value of the clicked button
dataType: 'json', // Choosing a JSON datatype
})
.done(function(data) { // Variable data contains the data we get from serverside
console.log("j");
})
.fail(function(data,error) {
console.log(data.responseText+error);
})
});
});
</script>parsererror"