In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.