weixin_33694620 2016-03-02 05:06 采纳率: 0%
浏览 67

找不到Laravel(ajax)404

I have searched online and the answer for this problem I found is to make sure that I am not using 2 gets or posts with the same name. But I'm not. I am just doing a quick tutorial online on how to use ajax with laravel but I get this error every time I try use post. When I use get it works fine. I would really appreciate it if someone could shed some light on this problem? Thanks.

POST http://ajaxlaravel.app:8000/register 404 (Not Found)

Here is my welcome.blade file:

<!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>

And my routes:

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

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

And my 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;
}
}
  • 写回答

2条回答 默认 最新

  • weixin_33682790 2016-03-02 05:08
    关注

    Change your post url to public/register or "<?php echo url('/register');?>"

    properly close your routes

    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()); 
            }
        });
    
    评论

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像