2017-05-30 13 views
2

に私はBLOCKCHAINのAPIのいずれかからデータを取得したい....なぜ、アクセス制御 - 許可 - 起源が動作しない、CodeIgniterの残り-サーバー

しかし、私は、私の小さな問題を取得私は 'AJAX GET REQUEST'を使って、CORS HEADERメソッドを使用してAPIと誤ってAPI BLOCKCHAINにアクセスしたいと思っています。 Asyc.js

$.getJSON("https://blockchain.info/rawaddr/1N1WJYDUgaBrk9eUfawSYurs9ZtKcVVfTE", function(data) { 
    console.log(data); 
}); 

コントローラCart.phpで

<?php 
defined('BASEPATH') OR exit('No direct script access allowed'); 

class Cart extends CI_Controller { 

    /** 
    * Index Page for this controller. 
    * 
    * Maps to the following URL 
    *  http://example.com/index.php/welcome 
    * - or - 
    *  http://example.com/index.php/welcome/index 
    * - or - 
    * Since this controller is set as the default controller in 
    * config/routes.php, it's displayed at http://example.com/ 
    * 
    * So any other public methods not prefixed with an underscore will 
    * map to /index.php/welcome/<method_name> 
    * @see https://codeigniter.com/user_guide/general/urls.html 
    */ 
    public function __construct() 
    { 
     parent::__construct(); 
     header("Access-Control-Allow-Methods: GET, OPTIONS"); 
     header("Access-Control-Allow-Headers: Content-Type, Content-Length, Accept-Encoding"); 
     if ("OPTIONS" === $_SERVER['REQUEST_METHOD']) { 
     die(); 
     } 
    } 
    public function index() 
    { 
     $this->load->view('cart_view'); 
    } 
} 

私はあなたがCORSがどのように機能するかを誤解しましたエラー

XMLHttpRequest cannot load https://blockchain.info/rawaddr/1N1WJYDUgaBrk9eUfawSYurs9ZtKcVVfTE. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. 
+0

'Controller Cart.php'は' https:// blockchain.info/rawaddr/1N1WJYDUgaBrk9eUfawSYurs9ZtKcVVfTE'や 'http:// localhost'を生成するファイルですか? – Quentin

答えて

2

を得ました。独自のコードにCORSヘッダーを追加しましたが、ヘッダーはblockchain.infoから出力する必要がありますが、もちろんアクセスできません。

ブロックチェーンapiコールの中には、CORSヘッダーを取得するのにこの特定のエンドポイントを取得するために&cors=trueを追加するものがあります。

解決方法は、独自のサーバー側からAPI呼び出しをプロキシすることです。ブロックチェーンAPIを呼び出すPHPコードのルートを作成します。サーバーからサーバーへの要求はCORSの対象にはなりません。 ajax経由であなたのPHPルートを呼び出します。

+0

コードの簡単な例を挙げることはできますか? – Firmansyah

+0

あなたのコントローラに、 'echo file_get_contents( 'https://blockchain.info/rawaddr/1N1WJYDUgaBrk9eUfawSYurs9ZtKcVVfTE')'のような新しいメソッドを作成し、blockchain.infoをロードするのではなくjavascriptからこのページを読み込みます – Antony

関連する問題