Django Django 与 Ajax-06

Introduction to Ajax

  AJAX (Asynchronous Javascript And XML) translated into Chinese is "Asynchronous Javascript and XML". That the use of Javascript language to interact with the server asynchronously,

  Transmitted data is XML (Of course, data transmission not only XML, now more use json data).

What is synchronous, asynchronous?

  • Synchronous interaction: The client sends a request, it must wait for the end of the server response before sending the second request;
  • Asynchronous interactions: the client sends a request to the server without waiting for the end of the response, it can issue a second request.

Why Ajax

  In addition to asynchronous AJAX features, and the other is: partial refresh browser page; (this feature to the user experience is the complete request and response process unknowingly)  

jquery Ajax-Based

  #btn Click to perform the function sends ajax / login /, returned by the backend data sussess callback function returns, verify that the correct user name jump page, add error handling local information

<Script> 

        $ ( ' #btn ' ) .click (function () { 
            User = $ ( ' to the #NAME ' ) .val (), 
            pwd = $ ( ' #pwd ' ) .val (), 
            $ .ajax ({ 
                URL: ' / Login / ' , // request URL 
                type: ' POST ' , // request method 
                Data: { ' User ' : User, ' pwd ' : pwd}, 
                Success:function (data) {
                    each data = JSON.parse(data);                

                    if(data.user){
                       location.href = 'http://www.baidu.com';
                    }else {
                       $('.error').html(data.msg).css('color','red')
                    }

                },

            });

        })
    </script>

      

Guess you like

Origin www.cnblogs.com/sunny666/p/11619802.html