I'm currently facing an issue with retrieving an ID from a JSON call and I'm unsure of what the problem might be. I have set up a Callback function to assign a global variable, but it doesn't seem to be working as expected.
OBJECTIVE: To query a database, retrieve the ID from the returned results.
STEP 1 - Initiate JSON Call and Parse Results
callAjaxGet(<set url>,function(myReturn){
var noteID = ''
$.each(myReturn.results, function(i, note){
noteID = JSON.parse(note.id);
});
})
STEP 2 - Handling JSON/Callback Function
function callAjaxGet(url, callBack){
$.ajax({
url: url,
type: 'GET',
timeout: 10000,
success: function(data,textStatus,xhr){
return callBack(xhr);
},
error: function(xhr, status, error){
console.log(xhr.responseText);
}
});
}
STEP 3 - JSON Data Being Returned
{
"next": "http://selleck.beta.org/playlist/notes/?limit=20&offset=20&play=437",
"previous": null,
"results": [
{
"id": 258,
"url": "/playlist/notes/258/",
"content": "testing",
"play": 437
}
]
}
Despite my efforts, the noteID
variable remains empty. I've checked multiple sources like Google Dev Tools and XHR, and although the JSON response is visible, I can't pinpoint where I may have gone wrong.
Any insights or suggestions would be greatly appreciated.
Regards, Steve