【发布时间】:2018-05-30 10:30:33
【问题描述】:
我在 localhost (xampp) / codeigniter 上有 api,我试图通过 angular 4 访问它,并且浏览器一直在控制台中显示此错误。 api 在邮递员中工作正常。
[火狐]:
跨域请求被阻止:同源策略不允许读取位于http://lvh.me/ci-abc/api/get_alltasks 的远程资源。 (原因:CORS 标头“Access-Control-Allow-Origin”缺失)。
[铬]:
无法加载http://lvh.me/ci-abc/api/get_alltasks:否 请求中存在“Access-Control-Allow-Origin”标头 资源。因此不允许使用原点“http://localhost:4200” 访问。
我也尝试将 API url 更改为 localhost 和 127.0.0.1,但没有成功。我是初学者,如果我遗漏了什么,请原谅我。
Angular 4 中的服务功能
gettasks()
{
return this.http.get('http://lvh.me/ci-abc/api/get_alltasks')
.map(res=>res.json());
}
codeigniter 中的 API 函数
function get_alltasks_get()
{
{
$result = $this->todo_model->get_alltasks();
if ($result) {
$this->response($result, 200);
} else {
$this->response("No record found", 404);
}
}
}
【问题讨论】:
标签: php angular api codeigniter