AJAX does not work with FireFox
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: AJAX does not work with FireFox

  1. #1
    Join Date
    Jan 2006
    Posts
    13

    AJAX does not work with FireFox

    Hi guys,

    i got a little problem. my ajax code doesn't work proper
    with IE it works great, but with FF it doesn't at all.
    for info: the php i call gives back text with the 'echo'-method. so no xml! but this should not be an issue.

    here is my code.
    the alerter "biste da?" i never saw with FF


    var response = "blabla" ;

    function ServerRequest(DBID)
    {
    sndReq(DBID);
    }

    function sndReq(DBID) {
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
    http = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
    http = new XMLHttpRequest();
    http.overrideMimeType('text/xml');

    }
    http.open('GET', 'http://localhost/fillme.php?action=' + DBID, false);

    alert(http.readyState );
    http.onreadystatechange = handleResponse;
    alert(http.readyState );
    http.send(null);
    }

    function handleResponse()
    {
    alert("biste da?");
    if(http.readyState == 4)
    {
    if (http.status == 200)
    {
    response = http.responseText;
    if(response.indexOf('|' != -1))
    {
    response = response.split("|||");
    response = response[1];
    }
    }
    }
    }


    Thanx in advance for help!!
    PL

  2. #2

    Re: AJAX does not work with FireFox

    AJAX should work fine with FireFox. How are you accessing the website? You need to make sure that your links are exactly the same as the address you initially type in (ie, http://localhost/ is different from http://127.0.0.1, etc.)

    You can also enable the JavaScript Console to see if there's anything wrong.

  3. #3
    Join Date
    Jan 2006
    Posts
    13

    Re: AJAX does not work with FireFox

    Hi,

    I am accessing the site with localhost as well.
    Since I am not a JS-Crack: where do I find that JS console?!?

    Thx,
    PL

  4. #4
    Join Date
    Jan 2006
    Posts
    13

    Re: AJAX does not work with FireFox

    hhhmmm. strange.
    I think i found the bug.
    FireFox doesn't like it when I set the 'asyncron' state on 'false' to tell the script to wait for the reply of the serverrequest.

    so instead of
    http.open("GET", destURL, true);
    I am using
    http.open("GET", destURL, false);

    but I NEED that feature!
    What can I do?

  5. #5
    Join Date
    Jan 2006
    Posts
    1

    Re: AJAX does not work with FireFox

    for that u have to use the other code just use this code for the mozilla

    <html>
    <head>
    <script type="text/javascript">
    var xmlDoc
    function loadXML()
    {
    //load xml file
    // code for IE
    if (window.ActiveXObject)
    {
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async=false;
    xmlDoc.load("note.xml");
    getmessage()
    }
    // code for Mozilla, etc.
    else if (document.implementation &&
    document.implementation.createDocument)
    {
    xmlDoc= document.implementation.createDocument("","",null);
    xmlDoc.load("note.xml");
    xmlDoc.onload=getmessage
    }
    else
    {
    alert('Your browser cannot handle this script');
    }
    }function getmessage()
    {
    document.getElementById("to").innerHTML=
    xmlDoc.getElementsByTagName("to")[0].firstChild.nodeValue
    document.getElementById("from").innerHTML=
    xmlDoc.getElementsByTagName("from")[0].firstChild.nodeValue
    document.getElementById("message").innerHTML=
    xmlDoc.getElementsByTagName("body")[0].firstChild.nodeValue
    }
    </script>
    </head><body onload="loadXML()" bgcolor="yellow">
    <h1>W3Schools Internal Note</h1>
    <p><b>To:</b> <span id="to"></span><br />
    <b>From:</b> <span id="from"></span>
    <hr />
    <b>Message:</b> <span id="message"></span>
    </p>
    </body>
    </html>

  6. #6
    Join Date
    Feb 2006
    Posts
    1

    Re: AJAX does not work with FireFox

    Hey specifying full paths for everything makes it work in IE, with partial paths, it works in netscape, opera, firefox.

    Can we send an argument to the function which handles the HTTP request?
    I had to change a dynamic variable, I got an alternative:
    1. Pass the name of the dynamic variable from HTML to Javascript as a string
    2. In JS make a global variable and assign it the string containing name to that dynamic variable
    3. Now in function u handle http request, use eval() to make Javascript code from the variable like eval(varname) = http.responseText

  7. #7
    Join Date
    Mar 2006
    Posts
    4

    Re: AJAX does not work with FireFox

    Player1005, i started using Ajax end of last year, but have never had any problems with it working in FF, actually mine seems to work better in FF than in IE. I also never had to put the 'asyncron' state on 'false'

    My ajax is in a seperate .js file: Will show how i did it, mayhaps it will help you in some way.

    My php also sends back the text using the 'echo' method without any xml!

    PHP Code:
    //create the object on loading of the page
    var http createRequestObject();

    function 
    createRequestObject()
    {
        var 
    request_o;
        var 
    browser navigator.appName;
        
        if(
    browser == "Microsoft Internet Explorer")
        {
            
    /* Create the object using MSIE's method */
            
    request_o = new ActiveXObject("Microsoft.XMLHTTP");
        }
        else
        {
            
    /* Create the object using other browser's method */
            
    request_o = new XMLHttpRequest();
        }
        return 
    request_o//return the object

    The function called when the appropriate button is pressed in html:
    PHP Code:
    function getText(el)
    {
      
    http.open('POST','printPHP.php?el='+eltrue);
      
    http.onreadystatechange handleText;
      
    http.setRequestHeader('Content-Type''application/x-www-form-urlencoded');
      
    http.send("printPHP.php?el="+el);

    And the function to handle the response:
    PHP Code:
    function handleText()
    {
      if(
    http.readyState == 4)
      {
          if(
    http.state == 200)
          {
            var 
    response http.responseText;
            
    document.getElementById('textDisplayArea').innerHTML response;
          }
      }  

    This might not be a direct answer to your problem, but as i said earlier, perhaps it can help you in some way.

  8. #8
    Join Date
    Mar 2006
    Posts
    3

    Angry Re: AJAX does not work with FireFox

    Well, I'm using the same exact code and it does not work with FF 1.5.0.1
    so.. nothing left to do ah yes! resignation.

  9. #9
    Join Date
    Mar 2006
    Posts
    1

    Re: AJAX does not work with FireFox

    Had the same problem myself, and then found this! Firefox won't allow you to use XMLHttpRequest to access a different domain. See the link below.

    http://www.captain.at/howto-ajax-per...ttprequest.php

    My workaround is to create a passthrough script on my server, so the request acts something like this.

    browser -> my server -> remote server -> my server -> browser

    A little bit annoying, but hey, that's client-side web development!

  10. #10
    Join Date
    Mar 2006
    Posts
    3

    Question Re: AJAX does not work with FireFox

    and did it work?
    could you provide a specific example?

  11. #11
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: AJAX does not work with FireFox

    another workaround is to use an invisible IFRAME. although, the target page must call a specific function (handle response) from the main/calling page just to notify that the document is ready. though i am not sure if some of the scripting requirements will work with firefox.
    Busy

  12. #12
    Join Date
    Mar 2006
    Posts
    3

    Re: AJAX does not work with FireFox

    Yes, we could give it a try...
    however i'm not even doing the request to a different domain (it is still localhost)

  13. #13
    Join Date
    Oct 2005
    Location
    India
    Posts
    24

    Resolved Re: AJAX does not work with FireFox

    The problem is with your Mozillla version.Its older than 1.1.Try using latestversion

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center