Django and Ajax
01.AJAX Introduction:
AJAX (Asynchronous JavaScript and XML) is translated into Chinese as "Asynchronous JavaScript and XML."
* Synchronous Interaction: After a client makes a request, it needs to wait for the server response to complete before issuing a second request
* Asynchronous Interaction: After a client makes a request, it can issue a second request without waiting for the server response to end
*ajax In addition to the characteristics of async, there is another is: Browser page local Refresh
Advantages:
Ajax uses JavaScript technology to send asynchronous requests to the server
Ajax does not need to refresh the entire page
02.ajax Sending JSON data
1 //Send JSON data2 $ ('. btn '). Click (function () {3 4 $.ajax ({5 URL: "",6 type: "Post",7 contentType: "Application/json",//The default contentType is urlencoded8 data:JSON.stringify ({//equivalent to Python in Json.dumps9 A:1,Ten B:2 One }), A success:function (data) { - - } the }) -})
03.ajax Uploading Files
1 <form>2User name<inputtype= "text"ID= "User">3Avatar<inputtype= "File"ID= "Avatar">4 <inputtype= "button"ID= "Ajax-submit"value= "Ajax-submit">5 </form>6 7 <Script>8 $("#ajax-submit"). Click (function(){9 //get file Fixed formatTen varFormdata=NewFormData (); One Formdata.append ("User",$("#user"). Val ()); A Formdata.append ("avatar_img",$("#avatar")[0].files[0]);//JS gets the file first converted to DOM object, and then through. Files[0] Get - $.ajax ({ - the URL:"", - Type:"Post", - Data:formdata, - //upload the file must write these two lines!!! + ProcessData:false , //do not process data - ContentType:false, //do not set content type + A Success:function(data) { at console.log (data) - } - }) - }) - </Script>
12.Django and Ajax