开发者

$.ajax always returns success, even with errors

I'm currently trying to solve an odd problem. I always worked with ajax, json, etc but have no idea why that simple code below is not working.

Below is the full javascript and html code:

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
        <title>Testing Ajax</title>
    </head>

    <body>
      <script>
        $(function(){
          $('h1').click(function(){
            alert('clicked');          
            jQuery.ajax({
              url: "teste2.html",
              success: function(html){
                $('h1').append(html);
              },
              error : function(){
                alert('errou');
              }
            });
          });
        });
      </script>
      <h1>Json Funfando</h1>
    </body>
</html>

At the same folder I have a file teste2.html. Anyway, if I rename the url to something that doesn't exist (dasdasdsadsa.html for example) that $.ajax will still return success, even with the correct name file the returned files are always empty. The error handling, if I inserted, is never called.

Anyone has any idea why this is happening?

PS.: I tryed to put it on a simple onload function, changing jQuery.开发者_开发百科ajax to $.ajax


You probably have to change the way your server responds and make it return a different HTTP header that implies an error rather than a 200 OK.

Check what your server returns, and see that it's returning 404 status for non-existing pages.


@fabio - what is the actual response header? I have found that jQuery ajax and specific browsers doesn't not actually error on some responses. Here is the one that I'm personally familar with:

  • FF 3.6.8 returns an xhr.status === 0 when server replies with HTTP code if 301.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜