1

当从我的本地计算机加载 HTML 文件时,这个确切的代码有效,但在我的远程主机上运行时无效。我使用的代码与 xdr.js jQuery 插件中的代码相同。我打印出的错误对象的状态文本为“错误:访问被拒绝”。

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        if ( window.XDomainRequest ) {
            jQuery.ajaxTransport = function( s ) {
                if ( s.crossDomain && s.async ) {
                    if ( s.timeout ) {
                        s.xdrTimeout = s.timeout;
                        delete s.timeout;
                    }
                    var xdr;
                    return {
                        send: function( _, complete ) {
                            function callback( status, statusText, responses, responseHeaders ) {
                                xdr.onload = xdr.onerror = xdr.ontimeout = jQuery.noop;
                                xdr = undefined;
                                complete( status, statusText, responses, responseHeaders );
                            }
                            xdr = new XDomainRequest();
                            xdr.onload = function() {
                                callback( 200, "OK", { text: xdr.responseText }, "Content-Type: " + xdr.contentType );
                            };
                            xdr.onerror = function() {
                                callback( 404, "Not Found" );
                            };
                            xdr.onprogress = jQuery.noop;
                            xdr.ontimeout = function() {
                                callback( 0, "timeout" );
                            };
                            xdr.timeout = s.xdrTimeout || Number.MAX_VALUE;
                            xdr.open( s.type, s.url );
                            xdr.send( ( s.hasContent && s.data ) || null );
                        },
                        abort: function() {
                            if ( xdr ) {
                                xdr.onerror = jQuery.noop;
                                xdr.abort();
                            }
                        }
                    };
                }
            };

        }
        jQuery.support.cors = true;
        var hash = '9lWA8';
        $.ajax({
            url: 'https://api.imgur.com/3/album/'+hash,
            type: 'GET',
            dataType: 'json',
            cache: false,
            beforeSend: function(xhr){xhr.setRequestHeader('Authorization', 'Client-ID 60512304ac2e7ce');},
            success: function(data) {
                console.dir(data.data);
            },
            error: function(data) {
                console.dir(data);
            }
        });
    });
</script>
</head

<body>
</body>

</html>
4

0 回答 0