Trying to get my json data from a php file and display.
So far I was able to request the data with ajax and log the data it to the console. (At least one thing seems to be working).
Then I attempted to use a callback so my script will wait for the data before it executes the display function. I followed a tutorial for this step by step but I must be doing something wrong because in the inspector it throws an error that my jsonData is not defined.
Then I try to display the data but without the callback working properly it won't work.
I'm going to try and explain what I did:
1. I wait for the document to load before running my script
$(document).ready(scriptWrapper);
2. I wrap the whole thing up in with a single function
function scriptWrapper(){
displayJson();
}
3. I start the function with my callback parameter
function requestJson(_callback){
4. Request my data from my php file using ajax
$.ajax({
url: "/test/senate.php",
success: result,
});
5. Send the result of the data to the console.log
function result(jsonData){
console.log (jsonData);
}
6. This marks the end of the callback
_callback();
7. Start the displayJson function
function displayJson(){
8.
execute requestJson() with the showData() function as the parameter which I think means showData will wait for the callback before executing.
requestJson(showData());
9. This is the function that will display the json data in my output div.
function showData(){
$(".output").append(jsonData);
}
Any insight would be appreciated!
I have a live version here congress.digitango.com/test/results.php
The full code is:
<div class="output"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
function scriptWrapper(){
displayJson();
}
function requestJson(_callback){
$.ajax({
url: "/test/senate.php",
success: result,
});
function result(jsonData){
console.log (jsonData);
}
_callback();
}
function displayJson(){
requestJson(showData());
function showData(){
$(".output").append(jsonData);
}
}
$(document).ready(scriptWrapper);
</script>