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

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.

dongyi1777
dongyi1777 它甚至没有转到php文件,因为url是问题,它不断返回错误函数而不是成功函数
接近一年之前 回复
dongqiu7365
dongqiu7365 看看github.com/barryvdh/laravel-cors在Laravel中处理CORS。如果您仍然遇到问题,请提供请求和回复标题。
接近一年之前 回复
dongyingjiu0669
dongyingjiu0669 由于php代码中的语法错误,检查你的PHP代码
接近一年之前 回复

1个回答

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.

douchui1657
douchui1657 对不起,我的回答并不是很清楚。 我已经更新了。
12 个月之前 回复
doutuan8887
doutuan8887 是的,请阅读jelhan上面的评论。 安装barryvdh / laravel-cors库并发布供应商文件。 只需按照Github上的文档进行操作即可。
接近一年之前 回复
duanqian8867
duanqian8867 我现在刚刚添加了文件cors.php,我运行我的代码,还没有工作,还有什么我需要做的才能让它工作吗?
接近一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问