ajax 错误处理 $.ajax({url:"",data:"",success:function(){},method:'get',error:function()}{}});

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jQuery.get()</title>
<style type="text/css">
ul{border:1px solid black;list-style:none;
margin:0pt;padding:0pt;float:left;
font-family:Verdana,Arial,Helvetica,sans-serif;
font-size:12px;width:300px;}
li{padding:10px 5px;border-bottom:1px solid black;}
</style>
</head>
<body>
<label for="fileName">Enter file name to load:</label>
<input type="text" id="fileName" />
<input type="button" value="Load file" />
<p  id="result"></p>
<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
     $('input:button').click(function(){
          if($('#fileName').val()==''){
               $('#result').html('<span>Please provide a file name</span>');
               return ;
          }
          $.ajax({
               url:$('#fileName').val(),
               method:'get',
               success:function(data){
                    $('#result').html(data);
               },
               error:function(){
                    $('#result').html('<span>error</span>');
               }
              
               });
     });
});

</script>
</body>
</html>
 

.ajaxError()


.ajaxError( handler(event, jqXHR, ajaxSettings, thrownError) )Returns: jQuery

Description: Register a handler to be called when Ajax requests complete with an error. This is an Ajax Event.

Whenever an Ajax request completes with an error, jQuery triggers the ajaxError event. Any and all handlers that have been registered with the .ajaxError() method are executed at this time. Note: This handler is not called for cross-domain script and cross-domain JSONP requests.

To observe this method in action, set up a basic Ajax load request.

1
2
3

<button class="trigger">Trigger</button>
<div class="result"></div>
<div class="log"></div>

Attach the event handler to the document:

1
2
3

$( document ).ajaxError(function() {
$( ".log" ).text( "Triggered ajaxError handler." );
});

 

posted @ 2014-04-04 17:25  wint  Views(1717)  Comments(0Edit  收藏  举报