API 路由给出 404 错误:Laravel 5.3
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41971856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
API route gives 404 error: Laravel 5.3
提问by Pankaj
I was testing my api on godaddy server. But due to some reasons the route is not working and giving 404 error
我正在 Godaddy 服务器上测试我的 api。但由于某些原因,该路线无法正常工作并出现 404 错误
In order to make sure that the route exists...I tested it like using below code.
为了确保路线存在......我像使用下面的代码一样对其进行了测试。
$routeCollection = \Route::getRoutes();
foreach ($routeCollection as $value) {
echo $value->getPath();
}
and you can check the same here as well: http://pankajservers.in/when u type any thing in user name or password and when u see console...it will show u 404 api error and on local it works perfectly.
你也可以在这里检查:http: //pankajservers.in/当你在用户名或密码中输入任何东西时,当你看到控制台......它会显示你 404 api 错误并且在本地它完美地工作。
JQuery
查询
var data = {
"EmailAddress": $("input[name='EmailAddress']").val(),
"Password": $("input[name='Password']").val(),
"_token": "{{ csrf_token() }}"
};
$.ajax({
method: "POST",
url: "{!! route('AuthenticateUser') !!}",
cache: false,
async: true,
data: data,
success: function(result) {
return false;
},
error: function(result) {
return false;
}
});
HTML
HTML
<form method="POST" action="http://pankajservers.in/api/v1/AuthenticateUser"
accept-charset="UTF-8"
id="loginForm">
<input name="_token" type="hidden" value="GtSNw3bgzFO6jwi8IVFWnvymd2e8EqIXkAhPtPxb">
<input class="form-control" name="EmailAddress" type="text">
<input class="form-control" name="Password" type="password" value="">
<button type="submit" class="btn btn-primary">
</form>
POST Route
邮寄路线
Route::post('/AuthenticateUser',
array(
'uses' => 'API\UserManagement\Auth\Login\apiLoginController@AuthenticateUser',
'as' => 'AuthenticateUser'
)
);
Route Service Provider
路线服务商
Route::group([
'middleware' => 'api',
'namespace' => $this->namespace,
'prefix' => 'api/v1',
], function ($router) {
require base_path('routes/API/UserManagement/Auth/Login/Login.php');
});
回答by Namal
All API requests must be prefixed as '/api/' in your ajax requests.
在 ajax 请求中,所有 API 请求都必须以“/api/”为前缀。
Ex: http://example.local/api/items
回答by Stuart Wagner
Looks to me like you have a rewrite issue. I tried going to the route on your site, but I prepended /index.php
to the route path. That threw a Laravel method exception, which is expected when you visit a POST-only route. Here, check it out:
在我看来,您有重写问题。我试着去你网站上的路线,但我预先设定/index.php
了路线路径。这引发了 Laravel 方法异常,这是您访问仅 POST 路由时所预期的。在这里,检查一下:
http://pankajservers.in/index.php/api/v1/AuthenticateUser
http://pankajservers.in/index.php/api/v1/AuthenticateUser
I'm not sure whether you're using Nginx or Apache, but I suspect either the Nginx config or the Apache .htaccess isn't quite right.
我不确定您使用的是 Nginx 还是 Apache,但我怀疑 Nginx 配置或 Apache .htaccess 不太正确。
EDIT:
编辑:
Take a look at Laravel's installation documentation. There's a section about rewrite configuration that might give you some insight:
看看 Laravel 的安装文档。有一个关于重写配置的部分可能会给你一些见解:
https://laravel.com/docs/5.4/installation#web-server-configuration
https://laravel.com/docs/5.4/installation#web-server-configuration
回答by Sunil Kumar
you can achieve by changing 'prefix' => 'api/v1'
, to 'prefix' => 'v1'
,
你可以通过改变'prefix' => 'api/v1'
,到'prefix' => 'v1'
,
as in laravel api
is assumed in the url of api.php
route.
就像在laravel中一样,api
在api.php
路由的url中假设。