0

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); 
        }
    });
}
3
  • No, now you return an HttpResponse object that has a JsonResponse object as payload. You serialize your data dict twice. Either return HttpResponse(response_data, content_type='application/json') or JsonResponse(response_data) Commented Jun 14, 2018 at 9:47
  • can you try var data = JSON.parse(response_data); Commented Jun 14, 2018 at 11:14
  • I have tried but i get the following error VM1764:1 Uncaught SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse (<anonymous>) Commented Jun 14, 2018 at 11:20

2 Answers 2

1

The dataType parameter of the ajax method in jQuery sets the type of the data you are expecting back from the server. You have set it to "text", which means that jQuery will treat the response just as pure text. This is not useful, especially if you want to treat it as an object and iterate through its keys.

Although you have not shown your view, which would have been useful, I presume you are actually sending JSON. In which case, you should set that parameter to "json" - or leave it out completely, in which case jQuery will make an intelligent guess.

Sign up to request clarification or add additional context in comments.

Comments

0

What you get from Django depends on what you return in your views. Using the render() shortcut, you get an HttpResponse object.

For Ajax, I tend to return JsonResponse objects.

If you return pure Querysets, you'll have to pass them through a serializer first.

3 Comments

I have tried to it but my problem is getting the data out of that json , it's not working for me using jquery, but if i just manuallly create an object jquery works ,i cannot explain that behaviour
Then why do you tag your question with python and django if you have trouble on the javascript/jquery side? Refer to @Daniel Roseman's answer then.
i apoligize for the inconvinience,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.