user641399
user641399

Reputation: 51

JQuery to test if URL is accessible

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

Upvotes: 0

Views: 2497

Answers (2)

user641399
user641399

Reputation: 51

<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>

Upvotes: 0

Manse
Manse

Reputation: 38147

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

Upvotes: 1

Related Questions