views:

94

answers:

2

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"&gt;
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
        <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

+3  A: 

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.

meder
A: 

@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.
Gutzofter