django restframework jquery post code example
Example 1: django restframework jquery post
$.ajax({
type:'POST',
url:'api/v1/comments/',
contentType: 'application/json',
data:{
post_id:$('#post_id').val(),
origin_path:$('#origin_path').val(),
parent_id:$('#parent_id').val(),
csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]').val()
},
success:function(json){
Example 2: django restframework jquery post
$('.apireq').click( function() { $.ajax({ url : "http://localhost:8000/studentsapi", dataType: "json", success : function (data) { $('#first_name').text( data[0].first_name); $('#last_name').text( data[0].last_name); $('#age').text( data[0].age); $('#gender').text( data[0].gender); } }); });
Example 3: django restframework jquery post
function newModule() {
var my_data = $("#my_element").val();
$.ajax({
url: "{% url 'modules' %}",
type: "POST",
dataType: "json",
data: {
path: my_data,
csrfmiddlewaretoken:
'{{ csrf_token }}'
},
success: function (json) {
},
error: function (xhr, errmsg, err) {
}
});
Example 4: django restframework jquery post
<script>
$('#commentForAjax' ).submit(function(e){
e.preventDefault();
$.ajax({
type:'POST',
url:'comment/create/',
data:$(this).serialize(),
success:function(json){
alert(json.message);
if(json.status==200){
var comment = json.comment;
var user = json.user;
}
},
error:function(response){
alert("some error occured. see console for detail");
}
});
});
Example 5: 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");
}
});
});