我正在与Laravel开发REST服务,以从移动应用程序中使用它。它可以在本地正常运行,但不能在主机上运行。经过几次尝试,我开发了一个基本示例来测试POST方法,但是它返回相同的错误。
api.php 文件
Route::post('/test', 'testController@test') ;
testController.php 文件
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class testController extends Controller
{
//
public function test(Request $request)
{
return response()->json(['mensaje' => 'POST access successful']);
}
}
POST请求始终返回相同的错误,我在请愿书上使用POST: 405方法不允许。该路由不支持GET方法。支持的方法:POST 。
我已经研究了这个主题,并且我读到它可能是由于CORS引起的。因此,我已经安装了spatie/laravel-cors及其默认配置,但POSTMAN仍然显示相同的错误。请帮忙吗?
已解决: 谢谢大家!绝对不是CORS问题。默认情况下,我的托管服务器会进行重定向,这样会丢失POST参数。
答案 0 :(得分:1)
答案 1 :(得分:0)
这是因为CORS(跨源资源共享)受到保护,因此您不允许从其他源调用api。要允许在api.php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE');
header('Access-Control-Allow-Headers: Content-Type, X-Auth-Token, Origin, Authorization');
答案 2 :(得分:0)
确定要调用/api/test
,因为在api.php路由文件中,路由上有前缀
答案 3 :(得分:0)
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException 该路由不支持GET方法。支持的方法:POST。 http://localhost:8000/save-product
23
C:\xampp\htdocs\firstProject\vendor\laravel\framework\src\Illuminate\Routing\RouteCollection.php
Illuminate\Routing\RouteCollection
:256
22
C:\xampp\htdocs\firstProject\vendor\laravel\framework\src\Illuminate\Routing\RouteCollection.php
Illuminate\Routing\RouteCollection
:242
21
Illuminate\Routing\RouteCollection
:176
20
Illuminate\Routing\Router
:634
19
Illuminate\Routing\Router
:623
18
Illuminate\Routing\Router
:612
17
Illuminate\Foundation\Http\Kernel
:176
16
Illuminate\Pipeline\Pipeline
:130
15
Illuminate\Foundation\Http\Middleware\TransformsRequest
:21
14
Illuminate\Pipeline\Pipeline
:171
13
Illuminate\Foundation\Http\Middleware\TransformsRequest
:21
12
Illuminate\Pipeline\Pipeline
:171
11
Illuminate\Foundation\Http\Middleware\ValidatePostSize
:27
10
Illuminate\Pipeline\Pipeline
:171
9
Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode
:62
8
Illuminate\Pipeline\Pipeline
:171
7
Fideloper\Proxy\TrustProxies
:57
6
Illuminate\Pipeline\Pipeline
:171
5
Illuminate\Pipeline\Pipeline
:105
4
Illuminate\Foundation\Http\Kernel
:151
3
Illuminate\Foundation\Http\Kernel
:116
2
:55
1
:21
Illuminate \ Routing \ RouteCollection :: methodNotAllowed:256 C:\ xampp \ htdocs \ firstProject \ vendor \ laravel \ framework \ src \ Illuminate \ Routing \ RouteCollection.php:256
$this->methodNotAllowed($methods, $request->method());
}
/**
* Throw a method not allowed HTTP exception.
*
* @param array $others
* @param string $method
* @return void
*
* @throws \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
*/
protected function methodNotAllowed(array $others, $method)
{
throw new MethodNotAllowedHttpException(
$others,
sprintf(
'The %s method is not supported for this route. Supported methods: %s.',
$method,
implode(', ', $others)
)
);
}
/**
* Get routes from the collection by method.
*
* @param string|null $method
//产品路线在这里
路线:: get('/ add-product','ProductController @ index');
路线:: post('/ save-product','ProductController @ save_product'); enter code here