我的AJAX请求的CORS策略错误与PHP
我试图在我的Laravel API服务器上发送AJAX请求,以更新用户数据,但会遇到CORS Orgin错误。我正在使用智能模板引擎。
错误:
在'http://127.0.0.0.1:8000/api/v2.3/user/updateuserroles'from Oncount'http:// localhost'已被CORS策略阻止:请求Header field acconer field-field accontrol' - 允许使用前响应中的访问控制允许头不允许使用。
这是我的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;
});
I tried to send a POST ajax request on my laravel API server to update users data but getting CORS Orgin Error. I am using Smarty Template Engine.
Error:
Access to XMLHttpRequest at 'http://127.0.0.1:8000/api/v2.3/user/updateUserRoles' from origin 'http://localhost' has been blocked by CORS policy: Request header field access-control-allow-methods is not allowed by Access-Control-Allow-Headers in preflight response.
Here is my Ajax request
$('.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;
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将其添加到您的文件请求中添加在代码顶部
add this in your file request add on top of your code