Laravel 5 CORS - XMLHttpRequest无法加载http://myapi.com。预检的响应具有无效的HTTP状态代码500 [英] Laravel 5 CORS - XMLHttpRequest cannot load http://myapi.com. Response for preflight has invalid HTTP status code 500

查看:2128
本文介绍了Laravel 5 CORS - XMLHttpRequest无法加载http://myapi.com。预检的响应具有无效的HTTP状态代码500的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Laravel 5和一个名为CORS的库。我面对一个错误,我不真正明白它是什么,为什么会发生。



我正在开发的API,将被网站的前端-end和一个应用程序。



我想对api进行ajax调用:

  $。ajax({
url:'http://myapi.com/call',
type:'GET',
headers:{'Authorization' :'Bearer token123'},
crossDomain:true,
contentType:'application / x-www-form-urlencoded; charset = UTF-8',
success:function console.log(data);},
error:function(xhr){console.log(xhr)}
});

但是我得到了这个错误:

  XMLHttpRequest无法加载http://myapi.com/call。响应预检具有无效的HTTP状态代码500 

但是,当我从请求中删除以下行时罚款。

  headers:{'Authorization':'Bearer token123'},



编辑:my config / cors.php

  'supportsCredentials'=> false,
'allowedOrigins'=> ['*'],
'allowedHeaders'=> ['*'],
'allowedMethods'=> ['GET','POST','PUT','DELETE','OPTIONS'],
'exposedHeaders'=> [],
'maxAge'=> 0,
'hosts'=> [],

有关如何解决这个问题的任何想法吗?

 > #一个方法是在.htaccess中添加以下内容(相应地修改)

头添加Access-Control-Allow-Origin*
头添加访问-Control-Allow-Headersorigin,x-requested-with,content-type
Header add Access-Control-Allow-MethodsPUT,GET,POST,DELETE,OPTIONS

#其他选项你可以参考以下链接laracasts

来源: https://laracasts.com/discuss/channels/requests/laravel -5-cors-headers-with-filters /?page = 2


I am using Laravel 5 and a library called CORS. I am facing an error that I don't really understand what it is and why it is happening.

I am developing an API that is gonna be used by a website's front-end and an app.

I am trying to do an ajax call to the API:

$.ajax({
  url: 'http://myapi.com/call',
  type: 'GET',
  headers: {'Authorization' : 'Bearer token123'},
  crossDomain: true,
  contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
  success   : function(data) { console.log(data);},
  error: function(xhr) {console.log(xhr)}
});

However I got this errro:

XMLHttpRequest cannot load http://myapi.com/call. Response for preflight has invalid HTTP status code 500

However, when I remove the following line from the request works fine.

headers: {'Authorization' : 'Bearer token123'},

EDIT: my config/cors.php

'supportsCredentials' => false,
'allowedOrigins' => ['*'],
'allowedHeaders' => ['*'],
'allowedMethods' => ['GET', 'POST', 'PUT',  'DELETE', 'OPTIONS'],
'exposedHeaders' => [],
'maxAge' => 0,
'hosts' => [],

Any idea about how to solve it?

解决方案

Did you configure CORS on server in laravel, there are couple of option to do the same

# one of the way is to add below in .htaccess (modify accordingly)

Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"

# other options you can refer to below link of laracasts

Source: https://laracasts.com/discuss/channels/requests/laravel-5-cors-headers-with-filters/?page=2

这篇关于Laravel 5 CORS - XMLHttpRequest无法加载http://myapi.com。预检的响应具有无效的HTTP状态代码500的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆