HMVC Codeigniter 中没有 'Access-Control-Allow-Origin' 错误

No 'Access-Control-Allow-Origin' error in HMVC Codeigniter

当页面在我的控制台中加载时显示此错误

Font from origin 'http://example.com' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.example.com' is therefore not allowed access.

并且当我调用 ajax 函数时。它显示此错误

XMLHttpRequest cannot load http://example.com/index.php/home/getCity?city=d. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.example.com' is therefore not allowed access.

哪些 ajax 功能在家庭控制器中。但我在另一个控制器中。可能是我认为这是问题。我该如何解决这个问题?

对于 Codeigniter 中的跨源资源,您必须在 <?php 标记

开始后立即将此行包含在控制器中
header('Access-Control-Allow-Origin: *');
<?php
header('Access-Control-Allow-Origin: *');
class Myclass extends CI_Controller {

}

这在 codeigniter 中工作得很好