Php 通过AJAX在Ubuntu18.04上安装CORS-Laravel

Php 通过AJAX在Ubuntu18.04上安装CORS-Laravel,php,laravel,cors,Php,Laravel,Cors,我有两个本地域(domainone.io和domaintwo.io),我想允许两者之间的COR 在服务器上,我确实通过以下方式允许它: server { listen 80; listen [::]:80; server_name domainone.io; root /media/....../domain1/public; index index.php index.html; location ~ \.php$ { i

我有两个本地域(domainone.io和domaintwo.io),我想允许两者之间的COR

在服务器上,我确实通过以下方式允许它:

server {
    listen    80;
    listen [::]:80;
    server_name domainone.io;
    root /media/....../domain1/public;
    index index.php index.html;

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        # With php7.0-cgi alone:
        #fastcgi_pass 127.0.0.1:9000;
        # With php7.0-fpm:
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    }

    location / {
        add_header 'Access-Control-Allow-Origin' 'domaintwo.io';
        add_header 'Access-Control-Allow-Methods' 'POST';
        add_header 'Access-Control-Allow-Methods' 'Content-Type';
        try_files $uri $uri/ /index.php?$query_string;
    }
}
在第二个领域:

server {
    listen    80;
    listen [::]:80;
    server_name domaintwo.io;
    root /media/....../domain2/public;
    index index.php index.html;

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        # With php7.0-cgi alone:
        #fastcgi_pass 127.0.0.1:9000;
        # With php7.0-fpm:
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    }

    location / {
        add_header 'Access-Control-Allow-Origin' 'domainone.io';
        add_header 'Access-Control-Allow-Methods' 'POST';
        add_header 'Access-Control-Allow-Methods' 'Content-Type';
        try_files $uri $uri/ /index.php?$query_string;
    }
}
问题:

当我从任一域进行ajax调用时,仍然会收到CORS not enabled错误消息,特别是:

已阻止跨源请求:同一源策略不允许读取 位于的远程资源。(理由:CORS 标题“访问控制允许来源”缺失)

我的JS类似于:

<script>
$(document).on('click', '.btn', function() {
  var someX = "lol";
  var token  = $('[name="csrf-token"]').prop("content");
  $.ajax({
    url: 'http://domaintwo.io/test',
    type: 'POST',
    data: {_token: token, test:someX },
    dataType: 'JSON',
    success: function(response) {
      alert(JSON.stringify(response));
    },
    error: function() {

    }
  });
});
</script>

$(文档).on('单击','.btn',函数(){
var someX=“lol”;
var token=$('[name=“csrf token”]')。prop(“content”);
$.ajax({
网址:'http://domaintwo.io/test',
键入:“POST”,
数据:{u令牌:令牌,测试:someX},
数据类型:“JSON”,
成功:功能(响应){
警报(JSON.stringify(响应));
},
错误:函数(){
}
});
});

我认为安装将解决您的问题

idk如果这会有帮助,但您有2个“访问控制允许方法”,第二个需要是“访问控制允许标题”,即使在修复了我收到的相同错误后,仍然可以使用浏览器的开发人员工具对您的请求进行bug,查看“网络”选项卡,查看您应用的CORS设置是否实际反映了。此外,在您的jQuery@et3rnal不,不要那样做。它不做你认为它做的事