I have been practising with pure Django (django without js) for a while now.I have therefore decided to go some step further to include Jquery Ajax in my django.Where i can be able to insert some data dynamically into my html.
The problem however is looping through the django model response using jquery ajax is not working for me. below is my jquery.Somebody help to through the array using jquery.
function ajaxLoad(item){
$.ajax({
type: 'POST',
url : '../ajax/fetch_category/',
data: {
'parameter':item
},
cache:false,
dataType: 'json',
success: function(response_data) {
//This is an example of respone data i get from ajax , though was created manually
var data = [
{
"model": "gallery.photogallery",
"pk": 2,
"fields":
{
"title": "This is my title",
"picture_choices": "Urban",
"description": "This is my good description .",
"user": 2,
"date_posted": "2018-06-13T20:13:57.774Z",
"views": 3,
"likes": 2,
"country": "AG",
"slug": "this-is-my-title"
}
}
];
//here am looping through the array
for( item in data){
console.log(item);
$.each(item, function( key, value ) {
console.log( key + ": " + value );
});
}
},
error: function(XMLHttpRequest,textStatus,errorThrown) {
console.log("XMLHttpRequest", XMLHttpRequest);
console.log("textStatus", textStatus);
console.log("errorThrown", errorThrown);
}
});
}
HttpResponse(response_data, content_type='application/json')orJsonResponse(response_data)var data = JSON.parse(response_data);