0

We are trying to determine whether it's possible to attempt to fetch a file/data from another domain (but trusted) via jQuery and determine whether the item was successfully fetched. In other words via this method we would want to test whether the user has set up this site as a trusted site in their browser. We did a test via img.src=[image on the 'another domain'], but it always succeeded. i.e. it didn't request authentication whether the trust was in place or not. So we are now looking for another solution / recommendation..

Thanks

Manse
  • 37,765
  • 10
  • 83
  • 108
user641399
  • 51
  • 9
  • Duplicate -> http://stackoverflow.com/questions/1051272/how-to-test-a-url-in-jquery – Manse Nov 17 '11 at 16:02
  • This is not a Duplicate!! kudos to you for the edit! - The atricle referred to only deals with accessing the page. to which the $get request will just give a success if the user responds to the authentication dialog or failure if they don't - We are looking for a solution whereby the user is not prompted for their credentials so we can silently test the success or failure to determine whether the site is trusted or not. – user641399 Nov 24 '11 at 17:27
  • Nope - the other question's answer links a plugin that will read the headers from a request - you will get 200 if the user can access the site - ie they are authenticated. You will receive 401 (http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html) if they are not .... that does exactly what you want ? I have added this as an answer (i did this the 16th But deleted it and linked to the other question) – Manse Nov 24 '11 at 17:31
  • Good answer: http://stackoverflow.com/questions/4301968/checking-a-url-in-jquery-javascript – Dilhan Jayathilake Oct 05 '14 at 23:22

2 Answers2

1

You could use the following plugin - http://binarykitten.me.uk/dev/jq-plugins/88-jquery-plugin-ajax-head-request.html

The function calls the passed url, passing the data and then processes the headers on completion.

you will get a status code of 200 if the user can access the site - ie they are authenticated. You will receive a status code of 401 if they are not

HTTP Status codes : http://w3.org/Protocols/rfc2616/rfc2616-sec10.html

Manse
  • 37,765
  • 10
  • 83
  • 108
  • Possibly, but this certainly doesnt appear to work in IE and we're restricted to this platform .comp policy.. – user641399 Nov 28 '11 at 17:04
  • As an update - it will capture the status code 404, but will always return a status of zero - if doing a cross domain request. the erorr is 'No transport' - i.e. a user with the trust will get this same error code as per users without the trust. – user641399 Nov 29 '11 at 10:54
  • NOTE: We can set the jQuery.support.cors = true; and this will enable xdomain calls and Indeed now we can test for a sucessfull call via the site in the trusted list and an unsucessfull call if the url is not in the tursted list. HOWEVER: by doing so the user is now prompted in all cases with 'This page is accessing information that is not under it's control. This poses a security risk. Do you want to continue?" which is going to be as annoying as hell to the users if we go with this soluiton. – user641399 Nov 29 '11 at 11:00
  • Setting the document.domain has fixed the prompt issue - so now i have a working test that determines if a url is accessable because it's in the users trusted domain. Excellent! code is posted – user641399 Nov 29 '11 at 12:20
0
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>Show Content</title>
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript"> 
            $(document).ready(function() {
                // this script resides on aaaa.yyyyy.domain.com
                document.domain='yyyy.domain.com';
                // call to trusted site
                var urlx="http://bbbb.yyyy.domain.com";

                // turn on x dom calls
                jQuery.support.cors = true; 

                // Launch AJAX request.
                $.ajax(
                    {
                        url: urlx,

                        // The type of request.
                        type: "head",

                        // The type of data that is getting returned.
                        dataType: "html",

                         error:function 
                         (xhr, ajaxOptions, thrownError){  
                            ShowStatus( "Call to URL: '"+urlx+ "' Failed "+xhr.status+" "+thrownError);
                         },
                        success: function( strData ){
                            ShowStatus( "Call to URL: '"+urlx+ "' Success");
                        }
                    }
                );
            });

            function ShowStatus( strStatus ){
                var jStatusList = $( "#ajax-status" );
                jStatusList.prepend( "<p>" + strStatus + "</p>" );
            }
        </script> 
    </head>
    <body>
        <div id="ajax-status" ></div>
    </body>
</html>
Stephen
  • 1,737
  • 2
  • 26
  • 37
user641399
  • 51
  • 9