How to use AJAX and JSON in Django
This article introduces how to use AJAX and JSON in Django. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
Conversion between JSON and other types
Stringify and parse method
JSON.parse (): used to convert a JSON string to a JavaScript object
JSON.parse ('{"name": "alex"}'); JSON.parse ('{name: "alex"}'); / / error JSON.parse ('[18 undefined]'); / / error
JSON.stringify (): used to convert JavaScript values to JSON strings
JSON.stringify ({"name": "alex"}) jQuery uses AJAX ajax test AJAX to test $("# ajaxTest") .click (function () {$.ajax ({url: "/ ajax_test/", type: "POST", data: {username: "Q1mi", password: 123456, hobby: JSON.stringify ([1, 2, 3])} Success: function (data) {alert (data)}})})
Views.py
Def ajax_test (request): user_name = request.POST.get ("username") password = request.POST.get ("password") print (user_name, password) res = {'status':200,' msg':'ok'} return JsonResponse (res) AJAX request how to set csrf_token
Send it in data by getting the value of csrfroomlewaretoken in the hidden input tag
$.ajax ({url: "/ cookie_ajax/", type: "POST", data: {"username": "Q1mi", "password": 123456, "csrfmiddlewaretoken": $("[name = 'csrfmiddlewaretoken']"). Val () / / use jQuery to take out the value of csrfmiddlewaretoken and splice it into data}, success: function (data) {console.log (data) }}) AJAX upload file / / upload file example $("# b3") .click (function () {var formData = new FormData (); formData.append ("csrfmiddlewaretoken", $("[name='csrfmiddlewaretoken']") .val ()); formData.append ("F1", $("# F1") [0] .files [0]) $.ajax ({url: "/ upload/", type: "POST", processData: false, / / tell jQuery not to process the sent data contentType: false, / / tell jQuery not to set the Content-Type request header data: formData Success:function (data) {console.log (data)}}) Django built-in serializers serialization returns data in JSON format def books_json (request): book_list = models.Book.objects.all () [0:10] from django.core import serializers ret = serializers.serialize ("json", book_list) return HttpResponse (ret) SweetAlert plug-in
Click to download the Bootstrap-sweetalert project
(".btn-danger") .on ("click", function () {swal ({title: "are you sure you want to delete?" Text: "you can't get it back if you delete it!" , type: "warning", showCancelButton: true, confirmButtonClass: "btn-danger", confirmButtonText: "delete", cancelButtonText: "cancel", closeOnConfirm: false}, function () {var deleteId = $(this). Parent (). Parent (). Attr ("data_id") $.ajax ({url: "/ delete_book/", type: "post", data: {"id": deleteId}, success: function (data) {if (data.status = 1) {swal ("deleted successfully!", "you are ready to run!" , "success");} else {swal ("deletion failed", "you can try again!" , "error")})});}) about how AJAX and JSON are used in Django, so much for sharing here. I hope the above content can help you to learn more. If you think the article is good, you can share it for more people to see.