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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog