CORS策略错误,我的ajax请求与PHP



我试图在我的laravel API服务器上发送POST ajax请求以更新用户数据,但获得CORS原点错误。我正在使用Smarty模板引擎.

错误:

从原点'http://localhost'访问'http://127.0.0.1:8000/api/v2.3/user/updateUserRoles'的XMLHttpRequest已被CORS策略阻止:请求头字段Access- control-allow-methods在预飞行响应中不被Access- control-allow- headers允许。

这是我的Ajax请求

$('.user_role').on('click',function(){
var role_id_and_user_id = $(this).val();
var func = 'delete';
if ($(this).is(":checked"))
{
func = 'insert'
}
console.log(func);
$('.loading').show();
var urlRemote = "{/literal}{$LARAVEL_BASEURL}{literal}/user/updateUserRoles";
$.ajax({
url: urlRemote,
type: "POST",
crossDomain: true,
data: {
'event': func,
'role_id': role_id_and_user_id,
},
cache: false,
contentType: false,
processData: false,
headers: {
'Authentication':"Bearer {/literal}{$smarty.session.laravel_token}{literal}",
"Accept": "application/json",
"Access-Control-Allow-Methods":"*",
},
success: function(responseData, textStatus, jqXHR) {
$('.loading').hide();
console.log(responseData);
},
error: function(jqXHR, textStatus, errorThrown) {
$('.loading').hide();
console.log(errorThrown);
}
})
return;
});

将此添加到您的文件请求中

<?php
header(“Access-Control-Allow-Origin: *”);
header(‘Access-Control-Allow-Methods: GET, POST’);

最新更新