“Django Restframework jQuery Post” Réponses codées

Django Restframework jQuery Post

$.ajax({
    type:'POST',
    url:'api/v1/comments/',  // switch to the API view url
    contentType: 'application/json',  // tell ajax to send it as `json` content
    data:{
      post_id:$('#post_id').val(),
      origin_path:$('#origin_path').val(),
      parent_id:$('#parent_id').val(),
      csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]').val()
    },
    success:function(json){
Concerned Chimpanzee

Django Restframework jQuery Post

<script>
     $('#commentForAjax' ).submit(function(e){
      e.preventDefault();

      $.ajax({
        type:'POST',
        url:'comment/create/',  // make sure , you are calling currect url
        data:$(this).serialize(),
        success:function(json){              
          alert(json.message); 
          if(json.status==200){
             var comment = json.comment;
             var user = json.user;
             /// set `comment` and `user` using jquery to some element
           }             
        },
        error:function(response){
          alert("some error occured. see console for detail");
        }
      });
     });
Concerned Chimpanzee

Django Restframework jQuery Post

    function newModule() {

        var my_data = $("#my_element").val(); // Whatever value you want to be sent.

        $.ajax({
            url: "{% url 'modules' %}",       // Handler as defined in Django URLs. 
            type: "POST",                     // Method.
            dataType: "json",                 // Format as JSON (Default).
            data: {
                path: my_data,                // Dictionary key (JSON). 
                csrfmiddlewaretoken: 
                         '{{ csrf_token }}'   // Unique key.
            },

            success: function (json) {

                // On success do this.

            },

            error: function (xhr, errmsg, err) {

                // On failure do this. 

            }

        });
Concerned Chimpanzee

Django Restframework jQuery Post

$('form').submit(function(e) {
    e.preventDefault();
    if ($(this).parents("tr") != 0) {
        parent_id = $(this).parents("tr").attr("id").split("_")[1];
        data_str = $(this).serialize() + "&parent_id=" + parent_id;
    } else {
        data_str = $(this).serialize();
    }
    $(this).parents("tr").attr("id").split("_")[1]
    $.ajax({
        type: 'POST',
        url: '{% url 'comment_create' %}',
        data: data_str,
        success: function(json) {
            alert(json.message);
            if (json.status == 200) {
                var comment = json.comment.trim();
                var user = json.user;
                if (!json.parent) {
                    $(comment).insertBefore('.table tr:first');
                } else {
                    $(comment).insertBefore('#comment_' + json.parent_id + ' #child_comment:first');
                    $(".replies").text("답글" + json.comment_count + "개 모두 보기");
                }
            }

        },
        error: function(response) {
            alert("some error occured. see console for detail");
        }
    });
});
Concerned Chimpanzee

Réponses similaires à “Django Restframework jQuery Post”

Questions similaires à “Django Restframework jQuery Post”

Plus de réponses similaires à “Django Restframework jQuery Post” dans JavaScript

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code