douguai4653 2018-09-25 11:42
浏览 97
已采纳

Access-Control-Allow-Origin'标题是角度laravel app上存在的错误

I have written simple code for angular as frontend and laravel as backend. I have code in my controller like:

PostController.php

public function store(Request $request)
{
   Post::create([
        'post_name'=>$request->post_name,
        'post_text'=>$request->post_text,

    ]);
    return response()->json(['msg'=>'Post Created','success'=>true]);
}

and in my routes.php file, I have:

Route::group([
    'middleware' => ['cors'],
], function ($router) {
     //Add you routes here, for example:
Route::resource('post','API\PostController');  
});

and in angular post.service.ts

addPost(data) {
      const headers = new HttpHeaders({
        'Accept': 'application/json',
        'Content-Type':'application/json'
      });
      let postdata={
        'post_name':data['post_name'],
        'post_text':data['post_text']
      };
      console.log(postdata);
      return this.http.post('http://192.168.1.100/crud/api/post',postdata,{ headers: headers });
  }

and after execution, I am getting an error like:

Failed to load http://192.168.1.100/crud/api/post: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.

  • 写回答

2条回答 默认 最新

  • douzhou7656 2018-09-26 04:11
    关注

    solved this problem by just adding chrome extension Moesif Origin & CORS Changer

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?