Laravel 5:尝试通过 API 访问数据时调用未定义的方法 Response::header()?
Laravel 5: Call to Undefined Method Response::header() when trying to access data through API?
我用 Laravel 和 CORS 中间件构建了一个 API。
<?php
namespace App\Http\Middleware;
use Closure;
class Cors
{
public function handle($request, Closure $next)
{
return $next($request)
->header('Access-Control-Allow-Origin', '*')
->header('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS')
->header('Access-Control-Allow-Headers','Content-Type, Authorization, X-XSRF-TOKEN');
}
}
尝试通过 API、localhost:8000/api/items
访问数据时,我在 Laravel 终端上得到以下 URL 和
Call to undefined method Symfony\Component\HttpFoundation\Response::header()
我错过了什么吗?
通过这种方式尝试,这应该可以解决您的 CORS 问题,将其声明到您的 class 的构造函数中。它会让你 API 工作。
<?php
namespace App\Http\Middleware;
use Closure;
class Cors
{
public function __construct(Request $request) {
$request->header('Access-Control-Allow-Origin', '*');
$request->header('Content-Type', 'text/plain');
}
}
为什么人们对这个答案投反对票?这项工作也比其他人好,我目前正在生产站点中使用,我是及时寻求帮助的人的第一个解决方案答案。
我知道有点晚了,但我在使用 Symfony\Component\HttpFoundation\StreamedResponse
时遇到了类似的问题。
正如你所说,问题是
Call to undefined method ... ::header()
很明显 header
方法不存在于对象上。
对我来说,解决方案是使用 headers
方法,returns 你 \Symfony\Component\HttpFoundation\ResponseHeaderBag
。
像这样使用它:
public function handle($request, Closure $next)
{
$response = $next($request);
$response->headers->set('Access-Control-Allow-Origin', '*');
$response->headers->set('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS');
$response->headers->set('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-XSRF-TOKEN');
return $response;
}
我用 Laravel 和 CORS 中间件构建了一个 API。
<?php
namespace App\Http\Middleware;
use Closure;
class Cors
{
public function handle($request, Closure $next)
{
return $next($request)
->header('Access-Control-Allow-Origin', '*')
->header('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS')
->header('Access-Control-Allow-Headers','Content-Type, Authorization, X-XSRF-TOKEN');
}
}
尝试通过 API、localhost:8000/api/items
访问数据时,我在 Laravel 终端上得到以下 URL 和
Call to undefined method Symfony\Component\HttpFoundation\Response::header()
我错过了什么吗?
通过这种方式尝试,这应该可以解决您的 CORS 问题,将其声明到您的 class 的构造函数中。它会让你 API 工作。
<?php
namespace App\Http\Middleware;
use Closure;
class Cors
{
public function __construct(Request $request) {
$request->header('Access-Control-Allow-Origin', '*');
$request->header('Content-Type', 'text/plain');
}
}
为什么人们对这个答案投反对票?这项工作也比其他人好,我目前正在生产站点中使用,我是及时寻求帮助的人的第一个解决方案答案。
我知道有点晚了,但我在使用 Symfony\Component\HttpFoundation\StreamedResponse
时遇到了类似的问题。
正如你所说,问题是
Call to undefined method ... ::header()
很明显 header
方法不存在于对象上。
对我来说,解决方案是使用 headers
方法,returns 你 \Symfony\Component\HttpFoundation\ResponseHeaderBag
。
像这样使用它:
public function handle($request, Closure $next)
{
$response = $next($request);
$response->headers->set('Access-Control-Allow-Origin', '*');
$response->headers->set('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS');
$response->headers->set('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-XSRF-TOKEN');
return $response;
}