我想从BLOCKCHAIN API中获取数据....为什么Access-Control-Allow-Origin在Codeigniter Rest-Server中不起作用
但是我遇到了一个小问题,想在使用'AJAX GET REQUEST'和dddddd taraaaaaaa后,使用'CORS HEADER'方法访问API和无意中的API BLOCKCHAIN,我得到错误:)。
在控制器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');
}
}
在Asyc.js
$.getJSON("https://blockchain.info/rawaddr/1N1WJYDUgaBrk9eUfawSYurs9ZtKcVVfTE", function(data) {
console.log(data);
});
我得到错误
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.
'Controller Cart.php'是生成'https:// blockchain.info/rawaddr/1N1WJYDUgaBrk9eUfawSYurs9ZtKcVVfTE'或'http:// localhost'的文件吗? – Quentin