I am calling JSON data and writing to it as follows:
var data;
$(function () {
$.getJSON("data.json", function (d) {
data = d;
});
$('.btn').click(function () {
data['c-type'] = $('#c-ccy option:selected').val();
data['f-type'] = $('#f-ccy option:selected').val();
$.ajax({
type: 'POST',
url: 'save2json.php',
data: {'json': JSON.stringify(data)},
success: function (msg) {
console.log('php output: ' + msg);
$.getJSON("data.json", function (d) {
console.log('GET JSON:');
console.log(d);
});
}
})
}); // <<THIS BLOCK OF CODE WORKS FINE AND READS/WRITES THE JSON OBJECT data PERFECTLY WHEN .btn IS CLICKED.>>
console.log(data); // <<ERROR: data undefined>>
})
Could someone please explain why is it that I can't access the data variable in my last line?