views:

431

answers:

3

I have a simple Ajax script that works perfectly fine in Chrome, Internet Explorer 8, Firefox 3.5.5 but fails in Firefox 3.5.7. The code is as follows:

HTML Page:

<div>
  <form>
    <input id='button' type='button' value='click'>
  </form>
</div>

<script>
  $(document).ready(function(){
    $("#button").click(function(){
      $.ajax({  
        type: "POST",  
        url: HTTP_HOST+"/ajax/ajax.php",  
        data: 'source=test',
        dataType: 'html',
        success: function(data) {alert('success: '+data);},
        error: function(XMLHttpRequest, textStatus, errorThrown){alert('fail:\n'+
                                                                 textStatus+'\n'+
                                                                 errorThrown+'\n'
                                                                 );}
      });
    });
  });
</script>

ajax.php page:

$sourcePage = $_POST['source'];
if($sourcePage == 'test'){
  echo 'hello';
}

I get the expected response (an alert box saying 'success: hello') in the browsers I mentioned above. In Firefox 3.5.7, however, I get the alert box with 'fail: error undefined'.

Using Firebug's Net panel I can see the Ajax calls and they get a response value of 200 OK although it can't seem to distinguish the size of the response.

There are no other errors according to Firebug.

Based on the apparently successful Ajax call and the undefined size of response, I'm assuming Firefox is having trouble interpreting the response though I'm new to this and have no idea what to try.

How can I solve this problem?

+1  A: 

EDIT:

Try changing your dataType to 'text'.

try this in your ajax.php:

$sourcePage = isset($_POST['source']) ? $_POST['source'] : 'NO SOURCE';

if($sourcePage == 'test'){
  echo 'hello';
}
else
{
  echo $sourcePage;
}
prodigitalson
Tried this but the problem still persists. The php bit seems to work fine considering that it works in the other browsers. Seems to be that it doesn't understand the response.
Dominic
Correct - thats why i had you change your PHP. if youre getting a response size of 0 then chances are php isnt outputting anything at all hence my default output of `$sourcePage`. It may be that thngs are getting mangled going in so that `$_POST['source']` doesnt exist. It may be that FF3.5.7 is having trouble with some of your markup or js in someway. Try using `empty()` instea of `isset()` an see if there is a difference.
prodigitalson
Sorry, i should have been more clear earlier. When i said it can't distinguish the size of the response i meant that it gives a value of '?' as opposed to thinking the file is 0 bytes. I have an else statement that echos 'brokenSource' in the php file so it should always be outputting something. Also, i have tried dataType: text but the problem persists.
Dominic
Hmmm can you try curl'ing down the page with the same POST vars and then looking at the raw response to see if anything is malformed?
prodigitalson
I tried using curl (though it's completely new to me and it's possible i did something wrong) and it seems to return the correct result(prints 'hello') in all browsers (including ff3.5.7)
Dominic
+1  A: 

I'm not sure if it matters but I have always passed data in the form:

data: {source: 'test'}

Also have you checked what you are getting in the response text of the XMLHttpRequest? In case that gives you any clues.

bobwah
I don't believe this is a problem as the php seems to work fine. According to the jQuery documentation the data option "is converted to a query string, if not already a string." I believe this means that your example will be converted to something like what i use e.g.'source=test'
Dominic
@Dominic: you are correct you can pass in in a manually encoded query string or an object which jQ will convert for you. That said, i always use object notation as well.
prodigitalson
+1  A: 

I found what the problem was. Basically what was happening is my HTML page had an address of http://domain.com.au/testPage.php but my Ajax was calling http://www.domain.com.au/ajax.php. There must be some type of security setting that complains when you try to make an Ajax call to a different domain?

So if I access the page as http://www.domain.com.au/testPage.php then it works. Also, if I change the Ajax to call http://domain.com.au/ajax.php without the www, and make the call from http://domain.com.au/testPage.php that also works.

Basically make sure the sub-domain of your page is the same as the sub-domain of the page you're requesting with Ajax.

This could potentially be a big problem for people who don't realise this as a lot of hosting packages (like mine) will serve you the same page with or without the www sub-domain by default. As such it is possible for me to access all of my pages with or without the www and not notice a difference (except that Ajax won't work).

It appears as though the other browsers have the same problem which means that it was a terrible fluke that every time I tested Firefox (on 5 different systems) I somehow ended up trying the URL without the www :(

I'll have to look into finding a way of forcing everyone to use the www sub-domain. I believe http://www.thesitewizard.com/apache/redirect-domain-www-subdomain.shtml#domaintosub is what I'm looking for.

Dominic
Oh yes that would do it.. you are sandboxed to a single domain with ajax unless you proxy the call.
prodigitalson