问题描述
我制作的小型REST API在本地运行良好。在服务器上传输时,GET请求所有工作,但POST不起作用。
我已经读过很多人都有这种效果,并且大多数时候问题是路由错误。
看起来像尾部的斜杠会导致重定向,从而导致问题。如果没有结尾斜杠,则无法进行重定向,并且可以执行API的POST。
不管是否有斜杠,有没有办法使这项工作有效?
这是.htaccess:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteBase /
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.PHP [L]
</IfModule>
这是我的路由和控制器:
$router->group(['prefix' => 'auth'],function ($router) {
$router->post('register','AuthController@register');
$router->post('login','AuthController@login');
$router->post('logout','AuthController@logout');
$router->post('refresh','AuthController@refresh');
$router->get('me','AuthController@me');
});
class AuthController extends Controller
{
public function __construct()
{
$this->middleware('auth:api',['except' => ['login','register']]);
}
public function login(Request $request)
{
$credentials = $request->only(['email','password']);
if (!$token = Auth::attempt($credentials)) {
return response()->json(['error' => 'Unauthorized'],401);
}
return $this->respondWithToken($token);
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)