【Python】flask框架 響應(yīng)前端ajax請(qǐng)求
            
            
              
                
                  - 
                    前端JavaScript代碼
                  
 
                  - 
                    后端Python代碼
                  
 
                
              
            
           
          
          
          
            前端JavaScript代碼
          
          
            
              
                <
              
              script src
              
                =
              
              
                "https://code.jquery.com/jquery-3.1.1.min.js"
              
              
                >
              
              
                <
              
              
                /
              
              script
              
                >
              
              
                <
              
              script type
              
                =
              
              
                "text/javascript"
              
              
                >
              
              
              
                alert
              
              
                (
              
              
                "GET"
              
              
                )
              
              
                ;
              
              
$
              
                .
              
              
                ajax
              
              
                (
              
              
                {
              
              
          type
              
                :
              
              
                "GET"
              
              
                ,
              
              
          url
              
                :
              
              
                "http://127.0.0.1:5000/login?pin=PIG"
              
              
                ,
              
              
              
                <
              
              
                !
              
              
                --
              
              dataType
              
                :
              
              
                "json"
              
              
                ,
              
              
                --
              
              
                >
              
              
          success
              
                :
              
              
                function
              
              
                (
              
              data
              
                )
              
              
                {
              
              
                alert
              
              
                (
              
              
                "GET:"
              
              
                +
              
              
                JSON
              
              
                .
              
              
                stringify
              
              
                (
              
              data
              
                )
              
              
                )
              
              
                ;
              
              
                }
              
              
                ,
              
              
          error
              
                :
              
              
                function
              
              
                (
              
              jqXHR
              
                )
              
              
                {
              
              
                alert
              
              
                (
              
              
                "Error: "
              
              
                +
              
              jqXHR
              
                .
              
              status
              
                )
              
              
                ;
              
              
                }
              
              
                }
              
              
                )
              
              
                ;
              
              
              
                alert
              
              
                (
              
              
                "POST"
              
              
                )
              
              
                ;
              
              
$
              
                .
              
              
                ajax
              
              
                (
              
              
                {
              
              
          type
              
                :
              
              
                "POST"
              
              
                ,
              
              
          url
              
                :
              
              
                "http://127.0.0.1:5000/login"
              
              
                ,
              
              
              
          dataType
              
                :
              
              
                "json"
              
              
                ,
              
              
          data
              
                :
              
              
                {
              
              
                "pin"
              
              
                :
              
              
                "wenru"
              
              
                ,
              
              
                "name"
              
              
                :
              
              
                "chenchao"
              
              
                }
              
              
                ,
              
              
          success
              
                :
              
              
                function
              
              
                (
              
              data
              
                )
              
              
                {
              
              
                alert
              
              
                (
              
              
                "POST:"
              
              
                +
              
              
                JSON
              
              
                .
              
              
                stringify
              
              
                (
              
              data
              
                )
              
              
                )
              
              
                ;
              
              
                }
              
              
                ,
              
              
          error
              
                :
              
              
                function
              
              
                (
              
              jqXHR
              
                )
              
              
                {
              
              
                alert
              
              
                (
              
              
                "Error: "
              
              
                +
              
              jqXHR
              
                .
              
              status
              
                )
              
              
                ;
              
              
                }
              
              
                }
              
              
                )
              
              
                ;
              
              
                <
              
              
                /
              
              script
              
                >
              
            
          
          
            后端Python代碼
          
          
            flask接收前端請(qǐng)求并返回JSON串
          
          
            
              views.py
            
          
          
            
              
              
                import
              
               json 
              
                from
              
               flask 
              
                import
              
               render_template
              
                ,
              
               request
              
                from
              
               app
              
                .
              
              handler 
              
                import
              
               errorres
              
                ,
              
               successres 
              
              
              
@home
              
                .
              
              route
              
                (
              
              
                "/login"
              
              
                ,
              
               methods
              
                =
              
              
                [
              
              
                "GET"
              
              
                ,
              
              
                "POST"
              
              
                ]
              
              
                )
              
              
              
                def
              
              
                login
              
              
                (
              
              
                )
              
              
                :
              
              
              
    requestArgs 
              
                =
              
               request
              
                .
              
              values  
    
              
              
              
    pin 
              
                =
              
               requestArgs
              
                .
              
              get
              
                (
              
              
                "pin"
              
              
                )
              
              
              
    pwd 
              
                =
              
               requestArgs
              
                .
              
              get
              
                (
              
              
                "pwd"
              
              
                )
              
              
              
                if
              
               pwd 
              
                ==
              
              
                None
              
              
                :
              
              
                print
              
              
                (
              
              
                ">>>>"
              
              
                ,
              
              
                "none pwd"
              
              
                )
              
              
                return
              
               errorres
              
                (
              
              
                "None pwd"
              
              
                )
              
              
                elif
              
               pin 
              
                ==
              
              
                None
              
              
                :
              
              
                print
              
              
                (
              
              
                ">>>>"
              
              
                ,
              
              
                "none pin"
              
              
                )
              
              
                return
              
               errorres
              
                (
              
              
                "None pin"
              
              
                )
              
              
                else
              
              
                :
              
              
              
        res 
              
                =
              
               jsonify
              
                (
              
              
                {
              
              
                "pin"
              
              
                :
              
               pin
              
                ,
              
              
                "pwd"
              
              
                :
              
               pwd
              
                }
              
              
                )
              
              
                return
              
               res         
            
          
          
            
              handler.py (這個(gè)沒啥用)
            
          
          
            
              
              
              
              
                from
              
               flask 
              
                import
              
               jsonify
              
                def
              
              
                errorres
              
              
                (
              
              msg
              
                )
              
              
                :
              
              
                return
              
               jsonify
              
                (
              
              
                {
              
              
                "msg"
              
              
                :
              
               msg
              
                ,
              
              
                "status"
              
              
                :
              
              
                0
              
              
                }
              
              
                )
              
              
                def
              
              
                successres
              
              
                (
              
              data
              
                ,
              
               msg
              
                =
              
              
                None
              
              
                ,
              
              
                )
              
              
                :
              
              
                return
              
               jsonify
              
                (
              
              
                {
              
              
                "msg"
              
              
                :
              
               msg
              
                ,
              
              
                "status"
              
              
                :
              
              
                1
              
              
                ,
              
              
                "data"
              
              
                :
              
               data
              
                }
              
              
                )