jquery form plugin, no error handling

前端 未结 5 2128
再見小時候
再見小時候 2020-12-24 14:26

It seems that there are no error handling facility in the Jquery.Form plugin, which is very frustrating. Even though the documentation says we can use the $.ajax options, I

5条回答
  •  独厮守ぢ
    2020-12-24 15:07

    Jquery Form Plugin handle the server response status success (code 200), i think that information is enough.

    From that you can create you response status server side

                return $this->returnJson(array(
                    'response' => 'success',//error or whatever
                    'data' => 'Succesfully saved in the database'
                ));
    

    of course returnJson is a function to send the Json data (you can build one or doing inside your method)

    in the frontend on success handle (is fired when response is 200) you just need to check your status field (in this case 'response'), example below:

      var options = {
            dataType : 'json',
            success: handleResponse
        };
    
    
        function handleResponse (responseText, statusText, xhr, $form){
    
              if(responseText.response == 'success') {
                      //do something
    
                    }else if(responseText.response == 'error'){
                        //do something
    
                }
    
    
        }
    

提交回复
热议问题