将<script>从域B传递到域A </script>

时间:2015-01-31 18:52:54

标签: javascript php

我正在使用进度条构建远程网址上传到其他服务器, 现在我有这两个域,即域A和B

我想将远程url文件从域A上传到域B,我在域B中有一个php函数来回显&lt;脚本&GT; parentFunction(); &LT;域A中的/ script&gt;

我知道由于安全问题,这在任何浏览器中都是不可能的,但我只想询问是否有针对此的解决方法

如果有人需要

,这是我在域B 中的php代码
function upload(url)
{
if ( function_exists( 'stream_context_create' ) )
    {
            $httpArr = array(
                'timeout' => 15, // 15 seconds
                );

            $httpArr[ 'notification' ] = [ $this, 'remote_url_progress_callback' ];

            $ctx = stream_context_create();
                stream_context_set_params( $ctx, $httpArr );
    }
}


function remote_url_progress_callback( $notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max )
    {
        switch($notification_code) {
            case STREAM_NOTIFY_RESOLVE:
            case STREAM_NOTIFY_AUTH_REQUIRED:
            case STREAM_NOTIFY_FAILURE:
            case STREAM_NOTIFY_AUTH_RESULT:
            break;

            case STREAM_NOTIFY_COMPLETED:
            echo '<script>parent.remoteProgress( '. json_encode( array( 'loaded' => $bytes_max, 'total' => $bytes_max ) ) .');</script>';
            break;

            case STREAM_NOTIFY_REDIRECTED:
            break;

            case STREAM_NOTIFY_CONNECT:
            echo '<script>parent.rstartTime = (new Date()).getTime();</script>';
            break;

            case STREAM_NOTIFY_FILE_SIZE_IS:
            echo '<script>parent.remoteProgress( '. json_encode( array( 'loaded' => 0, 'total' => $bytes_max ) ) .');</script>';
            break;

            case STREAM_NOTIFY_MIME_TYPE_IS:
            break;

            case STREAM_NOTIFY_PROGRESS:
            echo '<script>parent.remoteProgress( '. json_encode( array( 'loaded' => $bytes_transferred, 'total' => $bytes_max ) ) .');</script>';
            break;
        }
        ob_flush();
        flush();
    }

这是我在域A

中的代码
function remoteProgress( value )
{
    var percentageDone = parseInt( value.loaded / value.total * 100, 10 );
    nowTime = ( new Date() ).getTime();
    loadTime = ( nowTime - rstartTime );

    if ( loadTime == 0 )
    {
        loadTime = 1;
    }

    loadTimeInSec = loadTime / 1000
    bytesPerSec = value.loaded / loadTimeInSec;

    var textContent = '';
    textContent += 'Progress: '+percentageDone+'%';
    textContent += ' ';
    textContent += '('+bytesToSize(value.loaded, 2)+' / '+bytesToSize(value.total, 2)+')';
    textContent += '<br />Speed: '+ bytesToSize( bytesPerSec, 2 ) +'/s. ';

    progressText = textContent;
    $( '#remote-progress-bar' ).css( 'display', 'inline-block' );
    $( '#remote-progress-bar .bar').css(
        'width',
        percentageDone + '%'
    );

    $( '#remote-progress-text' ).html(progressText);
}

//域只是虚拟

$( '#remote-upload-progresser' ).prop( 'src', 'http://123.123.122.199/test/fileshare/remote/do-remote-upload/?remote[url_files]='+ encodeURIComponent( remote_files.join( "@@BREAK@@@" ) )  );

1 个答案:

答案 0 :(得分:0)

要在两个不同的网站上访问外部JavaScript,您需要添加一个CORS标头,如下所示:

Access-Control-Allow-Origin: *

目前尚不清楚您要实现的目标,但要启用此功能,您可以在PHP代码中添加header()函数调用。

header("Access-Control-Allow-Origin: *");

如果您只想在域B上允许来自域A的外部JavaScript,则可以将字符串修改为:

header("Access-Control-Allow-Origin: http://domainA.com");

请记住在将任何内容传递到客户端之前(即在任何echo声明之前)放置标题调用