Reputation: 166
I want to get data from one of BLOCKCHAIN API....
But I get a small problem, where I want to access the API and inadvertently API BLOCKCHAIN using 'CORS HEADER' method, after I use 'AJAX GET REQUEST' anddddddd taraaaaaaa I get Error :).
in Controller 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');
}
}
in Asyc.js
$.getJSON( "https://blockchain.info/rawaddr/1N1WJYDUgaBrk9eUfawSYurs9ZtKcVVfTE", function( data ) {
console.log(data);
});
i got error
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.
Upvotes: 2
Views: 732
Reputation: 64536
You've misunderstood how CORS works. You've added the CORS header to your own code whereas the header needs to be output from blockchain.info which of course you don't have access to.
Some of the blockchain api calls can have &cors=true
appended to get the CORS header but not this particular endpoint.
The solution is to proxy your api call through your own server side. Create a route in your php code that will call the blockchain api. The server to server request will not be subject to CORS. Call your php route via ajax.
Upvotes: 2