douxian1923 2019-07-12 17:22
浏览 356

跨源请求已阻止:同源策略禁止读取远程资源不起作用

I'm new to Laravel and have gone through several solutions on tackoverflow, but to no avail. It's still showing the same error of Cross-Origin Request Blocked.

$(".cmd_submit").click(function(){
$.ajax({
    //crossOrigin: true,
    //header:{'Access-Control-Allow-Origin': '*'},
    type: 'POST',
    url:  'http://localhost/members/register',
    data: $('#form_reg').serialize(),
    success: function(data){
       alert(data);
    },
    error: function(data){
  alert(data);
    }

I also included this:

<?php header("Access-Control-Allow-Origin: *"); ?>

on my app.blade.php, but it still didn't work.

  • 写回答

1条回答

  • duanjuelian4640 2019-07-12 18:05
    关注

    You need to set config/cors.php content.

    Mine looks like this:

    return [
    
        /*
        |--------------------------------------------------------------------------
        | Laravel CORS
        |--------------------------------------------------------------------------
        |
        | allowedOrigins, allowedHeaders and allowedMethods can be set to array('*')
        | to accept any value.
        |
        */
    
        'supportsCredentials' => false,
        'allowedOrigins' => ['*'],
        'allowedHeaders' => ['*'],
        'allowedMethods' => ['*'],
        'exposedHeaders' => [],
        'maxAge' => 0,
    
    ];
    

    UPDATE:

    I'm sorry I was not very clear here. You need to install barryvdh/laravel-cors library first and it will allow OPTIONS request to get 200 response with needed headers. Laravel does not come with this package so Chrome (or any other modern browser) will see that Cross-Origin headers are missing and will block the request.

    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退