Ajax Post in Django framework?

前端 未结 2 581
陌清茗
陌清茗 2021-02-06 14:30

I put together a simple test of an ajax/jquery post within a django framework, but don\'t really understand why the output doesn\'t make it to a template page. Anyone?

I

2条回答
  •  灰色年华
    2021-02-06 15:15

    I can see the contents of the post in firebug's 'response' tab, but whether I try to return a template or a simple message, nothing happens in the browser itself. Conversely, a non-ajax post works as expected (loads new page, posts message)

    If you're getting a response, you're getting a response. You're just not doing anything with it.

    Why not alert the data and append it to the for example:

    $.ajax({
         type:"POST",
         url:"/test_results/",
         data: {
                'arbitrary-data': 'this is arbitrary data',
                'some-form-field': $("myform input:first").val(), // from form
                'background-color': $("body").css("background-color")
                // all of this data is submitted via POST to your view.
                // in django, request.POST['background-color'] 
         },
         success: function(data){
             alert(data);
             $("body").append(data);
         }
    });
    

提交回复
热议问题