-1

外部 URL への AJAX リクエストを作成し、その URL から Json データを取得しようとしています。URL にアクセスしようとすると、コンテンツが保護されていないことを示す 401-Authorization エラーが発生します。以下は私のコードです。お知らせ下さい

function check() {

    var ENV_URL = 'http://../BlueLight/api/BlueLights/getActiveBlueLight';   
    $('#trBlueLight').hide();
    $.getJSON(ENV_URL+"&callback=?", function (data) {
        if (data.expDate != null) {
            $('#trBlueLight').show();
        } else {
            $('#trBlueLight').hide();
        }
    });
    }
4

2 に答える 2

1

だから、コメントを待ち望んでいます。次のファイルproxy_loader.phpを作成し、Webサーバーのルートディレクトリ(通常はhttpdocs)に配置すると仮定します。https://domain.tdl/proxy_loader.phpからアクセスできる必要があります

ファイルは次のようになります。

<?PHP

// get the url provided by javascriot
$url= $_GET['url'];

// initialize CURL
$ch = curl_init();

// additional headers like session id etc.
$header = '';

// optional user & password
$userpass = 'user:password';

//$parameters for the request
// use parameter name as array key
// use parameter value as array value
$param = array();

// set url
curl_setopt($ch, CURLOPT_URL, $url);
//this enables the output into a variable
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// set optional header
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// set request method to GET
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
// optional http authenfication
// remove the // to enable
// curl_setopt($ch, CURLOPT_USERPWD, $userpass);
curl_setopt($ch, CURLOPT_POSTFIELDS, $param);

// execute the request
// and save the response in $ret
$ret = curl_exec($ch);

// close the connection
curl_close($ch);

echo $ret;

// maybe you must use this:
// echo json_encode($ret);
?>

次のような関数を使用して、サーバー経由で URL をリクエストできます。

function check() {

    var ENV_URL = 'http://../BlueLight/api/BlueLights/getActiveBlueLight';   

    $('#trBlueLight').hide();

    $.getJSON('/proxy_loader.php?url=' + ENV_URL, function (data) {
        if (data.expDate != null) {
            $('#trBlueLight').show();
        } else {
            $('#trBlueLight').hide();
        }
    });
}
于 2013-05-03T07:50:50.103 に答える