showing results for - "django restframework jquery post"
Victoria
12 May 2016
1$.ajax({
2    type:'POST',
3    url:'api/v1/comments/',  // switch to the API view url
4    contentType: 'application/json',  // tell ajax to send it as `json` content
5    data:{
6      post_id:$('#post_id').val(),
7      origin_path:$('#origin_path').val(),
8      parent_id:$('#parent_id').val(),
9      csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]').val()
10    },
11    success:function(json){
12
Andrés
18 Jul 2019
1<script>
2     $('#commentForAjax' ).submit(function(e){
3      e.preventDefault();
4
5      $.ajax({
6        type:'POST',
7        url:'comment/create/',  // make sure , you are calling currect url
8        data:$(this).serialize(),
9        success:function(json){              
10          alert(json.message); 
11          if(json.status==200){
12             var comment = json.comment;
13             var user = json.user;
14             /// set `comment` and `user` using jquery to some element
15           }             
16        },
17        error:function(response){
18          alert("some error occured. see console for detail");
19        }
20      });
21     });
Safa
28 Jun 2018
1$('form').submit(function(e) {
2    e.preventDefault();
3    if ($(this).parents("tr") != 0) {
4        parent_id = $(this).parents("tr").attr("id").split("_")[1];
5        data_str = $(this).serialize() + "&parent_id=" + parent_id;
6    } else {
7        data_str = $(this).serialize();
8    }
9    $(this).parents("tr").attr("id").split("_")[1]
10    $.ajax({
11        type: 'POST',
12        url: '{% url 'comment_create' %}',
13        data: data_str,
14        success: function(json) {
15            alert(json.message);
16            if (json.status == 200) {
17                var comment = json.comment.trim();
18                var user = json.user;
19                if (!json.parent) {
20                    $(comment).insertBefore('.table tr:first');
21                } else {
22                    $(comment).insertBefore('#comment_' + json.parent_id + ' #child_comment:first');
23                    $(".replies").text("답글" + json.comment_count + "개 모두 보기");
24                }
25            }
26
27        },
28        error: function(response) {
29            alert("some error occured. see console for detail");
30        }
31    });
32});
33
Alessandra
03 Jul 2016
1    function newModule() {
2
3        var my_data = $("#my_element").val(); // Whatever value you want to be sent.
4
5        $.ajax({
6            url: "{% url 'modules' %}",       // Handler as defined in Django URLs. 
7            type: "POST",                     // Method.
8            dataType: "json",                 // Format as JSON (Default).
9            data: {
10                path: my_data,                // Dictionary key (JSON). 
11                csrfmiddlewaretoken: 
12                         '{{ csrf_token }}'   // Unique key.
13            },
14
15            success: function (json) {
16
17                // On success do this.
18
19            },
20
21            error: function (xhr, errmsg, err) {
22
23                // On failure do this. 
24
25            }
26
27        });
28