I'm able to parse an item in a Json array inside of an AJAX request, but outside it comes undefined. Why is this?
function samplesByDate() {
var csrftoken = getCookie('csrftoken');
$.ajax({
url : "/samples/byDate/",
type : "POST",
data : { csrfmiddlewaretoken : csrftoken },
success : function(json) {
if (json['error'] === 'true' ) {
alert(json['message']);
} else {
console.log(json['samples']);
return json['samples'];
}
},
error : function(xhr,errmsg,err) {
console.log(xhr.status + ": " + xhr.responseText);
}
})
};
The console log inside the ajax function reports correctly:
[
{
"id": 72,
"title": "Sample Upload 1",
"date": "2017-09-18",
"coordinates": "45.58837890625, -122.39273834228516",
"type_primary": "Brix",
"value_primary": 15.52022647857666,
"type_secondary": "Dry Matter",
"value_secondary": 16.35165786743164
}
]
But when I attempt to print the value after its been returned, its instead undefined.
console.log(samplesByDate());
With a console display of
undefined
One thing I've noted is that its not erroring that samplesByDate() is undefined, so its correctly iterating through the function, but rather display undefined in an attempt to print the return value.