PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, January 26, 2022

[FIXED] API route gives 404 error: Laravel 5.3

 January 26, 2022     laravel, laravel-5.3     No comments   

Issue

I was testing my api on godaddy server. But due to some reasons the route is not working and giving 404 error

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.

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

<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');
});

Solution

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:

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.

EDIT:

Take a look at Laravel's installation documentation. There's a section about rewrite configuration that might give you some insight:

https://laravel.com/docs/5.4/installation#web-server-configuration



Answered By - Stuart Wagner
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing