Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Request a Password Protected Page in Javascript

I'm working on a very simple Sidebar Gadget to analyze my router's monthly bandwidth usage and determine how far ahead or behind I am for that month. The data is located in a router-hosted, password protected webpage (I'm using DD-WRT, if it matters). I'd like to pass a page request to the router with Javascript, along with all the authentication information, to retrieve the page all in one go, but I can't seem to find the proper syntax for that. Any ideas?

Here's my code so far:

var ua = navigator.userAgent.toLowerCase();
        if (!window.ActiveXObject){
          req = new XMLHttpRequest();
        }else if (ua.indexOf('msie 5') == -1){
          req = new ActiveXObject("Msxml2.XMLHTTP");
        }else{
          req = new ActiveXObject("Microsoft.XMLHTTP");
        }

        req.open('GET', 'http://192.168.1.1/Status_Internet.asp', false, "username", "password");   
        req.send(null);  
        if(req.status == 200){
           dump(req.responseText);
        } else{
            document.write("Error");
        }

        document.write("Second Error");

Firebug indicates that it throws an error on req.send(null) - specifically, Access to restricted URI denied" code: "1012.

It may be because of the same-origin policy, but in that case what can I use instead of an xmlhttpRequest?

like image 638
Brian Bauman Avatar asked Jun 07 '26 12:06

Brian Bauman


1 Answers

It is because of the same-origin policy, the alternative is an iframe but that will not really give you what you wish for.

If it is http-auth you used to be able to request the page with http://username:pass@site but i must admit i haven't tried to use that for a long time, so i don't know if it is still supported.

EDIT:

If this doesn't work, maybe you can use basic http auth as described here: http://en.wikipedia.org/wiki/Basic_access_authentication but this would require you to sue a serverside proxy, since you can't manipulate the request headers from javascript when xhr is not an option.

like image 152
Martin Jespersen Avatar answered Jun 09 '26 00:06

Martin Jespersen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!