XMLHttpRequest

Discussion in 'Javascript' started by Tom de Neef, Mar 28, 2008.

  1. Tom de Neef

    Tom de Neef Guest

    1) The code below works fine with IE and FF when the requested file is
    local.
    2) But when I try to load the file from a http://www ... address IE does it
    and FF fails.

    So, from 1) I conclude that the code is correct. The XMLHttpRequest object
    is created, the "GET" works, etc.
    Since IE works in 2) I know that the file is accessible.
    I have given up to understand why FF fails in 2).
    Can you give it a look and advise me pls.
    Tom

    function LoadFile(how,url)
    {
    var req = false;
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest && !(window.ActiveXObject))
    { try { req = new window.XMLHttpRequest() }
    catch(e) { req = false }
    }
    else // branch for IE/Windows ActiveX version
    { if (window.ActiveXObject)
    { try { req = new ActiveXObject("Msxml2.XMLHTTP") }
    catch(e)
    { try { req = new ActiveXObject("Microsoft.XMLHTTP") }
    catch(e) { req = false }
    }
    }
    }

    if(req)
    { req.open(how, url, false)
    req.send(null)
    return req.responseText
    }
    return ""
    }

    LoadFile("GET",someFile)
     
    Tom de Neef, Mar 28, 2008
    #1
    1. Advertisements

  2. If you're testing this from a local file which is AFIAK the only way 1)
    works, FF will prevent this from working because of access
    restrictions. IIRC you should get a message which should give you the
    choice to allow the local page to access the remote one and/or enable
    UniversalBrowserRead. I can't seem to find the definitive document at
    the moment, but it's touched at here:

    http://developer.mozilla.org/en/docs/XML_Extras
     
    Joost Diepenmaat, Mar 28, 2008
    #2
    1. Advertisements

  3. Tom de Neef

    Tom de Neef Guest

    That was quick! And adequate.
    The problem was that I was testing the code from my PC. The httpRequest will
    not repond if it must load from another domain. So, you can not test loading
    a http://www. address unless the code runs on that site.
    All solved.
    Thank you.
    Tom
     
    Tom de Neef, Mar 28, 2008
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.