Laravel 5.2:POST 请求总是返回“405 Method Not Allowed" [英] Laravel 5.2: POST request is always returning "405 Method Not Allowed"

查看:100
本文介绍了Laravel 5.2:POST 请求总是返回“405 Method Not Allowed"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在使用 Laravel 5.2 开发 API,但我正面临一个重要问题.

So I am developing an API with Laravel 5.2 and I'm facing an important issue.

我有一个 UserController 来管理我的应用程序的用户.

I have a UserController that will manage the users of my app.

这是我的 routes.php 文件:

This is my routes.php file:

Route::group(array('prefix' => 'api/v1'), function() {    
   Route::post('user', 'UserController@store');
});

我的 UserController 定义如下:

And I have my UserController defined like that:

class UserController extends Controller {

   public function index() {
       return 'Hello, API';
   }

   public function create(){
   }

   public function store(Request $request) {
       $user = new User;
       $user->email = $request->email;
       $user->password = $request->password;
       $user->fbId = $request->fbId;
       $user->ggId = $request->ggId;
       $user->firstName = $request->firstName;
       $user->lastName = $request->lastName;
       $user->imageUrl = $request->imageUrl;
       $user->country = $request->country;
       $user->mobile = $request->mobile;
       $user->gender = $request->gender;
       $user->client = $request->client;

       $user->save();

       return Response::json(array(
           'error' => false,
           'userId' => $user->id),
           200
       );
   }

   public function update(Request $request, $id) {
   }
}

这是php artisan route:list

+--------+--------+-------------+------+-------------------------------------------+------------+
| Domain | Method | URI         | Name | Action                                    | Middleware |
+--------+--------+-------------+------+-------------------------------------------+------------+
|        | POST   | api/v1/user |      | App\Http\Controllers\UserController@store | web        |
+--------+--------+-------------+------+-------------------------------------------+------------+

我正在使用 Postman 来测试我的 POST 请求.每次我向/api/v1/user 发出 POST 请求时,都会收到405 Method Not Allowed"错误.

I'm using Postman to test my POST requests. Every time I make a POST request to /api/v1/user, I get a "405 Method Not Allowed" error.

我错过了什么吗?

我应该做些什么来解决这个问题?

Is there anything I should do to fix this issue?

推荐答案

我和你有同样的问题,我已经在我的 POST 路由中设置了例如/api/v1/user",

当我尝试使用 POSTMAN (application to test API) 进行连接时,它返回 405-Method Not Allowed,

I have same problem with you which are I already set in my POST route for example "/api/v1/user",

and when I try to connect using POSTMAN (application to test API) , it return 405-Method Not Allowed,

然后我意识到我发送的网址使用的是http",然后我将其更改为https"(后面带有s")
然后我的 API 正常工作!

and then i realize the url i was sent is using 'http' and after i change it to 'https' (with the 's' at the back)
then my API working like normal !

通常,如果我们与不同的服务器交互,我们必须使用https"
但如果您的应用程序在同一服务器上,
可以使用http'

我的情况的真正原因是与任何不同服务器的任何交互都必须使用https"(这是我服务器上的设置)

normally if we interact with different server , we must use 'https'
but if your application at the same server ,
it's okay to use 'http'

The real reason for my case is any interaction with any different server must use 'https' (This is the setup on my server)

这篇关于Laravel 5.2:POST 请求总是返回“405 Method Not Allowed"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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