【问题标题】:Laravel(ajax) 404 not foundLaravel(ajax) 404 未找到
【发布时间】:2016-03-02 05:06:12
【问题描述】:

我在网上搜索过,我发现这个问题的答案是确保我没有使用 2 个获取或同名的帖子。但我不是。我只是在网上做一个关于如何在 laravel 中使用 ajax 的快速教程,但是每次我尝试使用 post 时都会出现这个错误。当我使用 get 时,它工作正常。如果有人能对这个问题有所了解,我将不胜感激?谢谢。

POST http://ajaxlaravel.app:8000/register 404(未找到)

这是我的welcome.blade 文件:

<!DOCTYPE html>
<html>
<head>
    <title>Laravel</title>
    <meta name="csrf-token" content="{{ csrf_token() }}" />>

    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"/>

</head>
<body>
    <div class="container">
        <h2>Register form</h2>

        <div class="row col-lg-5">
            <h2>Get Request</h2>
            <button type="button" class="btn btn-warning" id="getRequest">getRequest</button>
        </div>

        <div class="row col-lg-5">
            <h2>Request Form</h2>
            <form id="register" action="#">
                {{ csrf_field() }}
                <label for="firstname">
                    Firstname: <input type="text" id="firstname" class="form-control" placeholder="John"/>
                </label><br>

                <label for="lastname">
                    Lastname: <input type="text" id="lastname" class="form-control" placeholder="Smith"/>
                </label><br><br>

                <input type="submit" value="Register" class="btn btn-primary">
            </form>
        </div>
    </div>



    <div id="getRequestData"></div>

    <div id="postRequestData"></div>

    <script type="text/javascript" src="{{asset('js/jquery.js')}}"></script>
    <script type="text/javascript">
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        $(document).ready(function(){
            $('#getRequest').click(function(){
                $.get('getRequest', function(data){ 
                    $('#getRequestData').append(data);
                    console.log(data);
                });
            });

            $('#register').submit(function(){
               var fname = $('#firstname').val();
                var lname = $('#lastname').val();

                $.post('register', {firstname:fname, lastname:lname}, function(data){ //Take data from form register and post it but give a call back function using data.
                    console.log(data);
                    $('#postRequestData').html(data);
                });
            });
        });
    </script>

</body>
</html>

还有我的路线:

Route::group(['middleware' => ['web']], function () {
Route::get('/', 'WelcomeController@index');

Route::get('/getRequest', function(){
    if(Request::ajax()){ //Does a check to see if the request is ajax.
        return 'getRequest loaded completely';
    }

    Route::post('/register', function(){
        if(Request::ajax()){
            return Response::json(Request::all()); 
        }
    });
});
});

还有我的 WelcomeController:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class WelcomeController extends Controller
{
public function _construct(){
    $this->middleware('guest');
}

/*
 * Show application review
 *
 * @return response
 */
public function index(){
    return view('welcome');
}
}

还有我的 VerifyCrsffToken.php:

<?php

namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;

class VerifyCsrfToken extends BaseVerifier
{
/**
 * The URIs that should be excluded from CSRF verification.
 *
 * @var array
 */
protected $except = [
    //
];

/**
 * Determine if the session and input CSRF tokens match.
 *
 * @param \Illuminate\Http\Request $request
 * @return bool
 */
protected function tokensMatch($request)
{
    // If request is an ajax request, then check to see if token matches token provider in
    // the header. This way, we can use CSRF protection in ajax requests   also.
    $token = $request->ajax() ? $request->header('X-CSRF-Token') : $request-    >input('_token');

    return $request->session()->token() == $token;
}
}

【问题讨论】:

  • 那个是注册页面吗?还是请求?

标签: php jquery ajax laravel


【解决方案1】:

将您的帖子网址更改为public/register"&lt;?php echo url('/register');?&gt;"

适当地关闭你的路线

Route::get('/getRequest', function(){
    if(Request::ajax()){ //Does a check to see if the request is ajax.
        return 'getRequest loaded completely';
    }});//here

    Route::post('/register', function(){
        if(Request::ajax()){
            return Response::json(Request::all()); 
        }
    });

【讨论】:

  • 都试过了,得到:POST ajaxlaravel.app:8000/public/register 404 (Not Found)
  • 问题在于路线关闭。我会在一分钟内接受你的回答。非常感谢你。我真的很感激。
【解决方案2】:

尝试将您的帖子网址更改为public/index.php/register

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-22
    • 1970-01-01
    • 2021-03-21
    • 2019-10-20
    • 2021-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多