在laravel 5.3中找不到404 Ajax url

时间:2016-10-30 10:56:43

标签: ajax laravel vue.js

我正在学习laravel。所以,如果我要求或做一些愚蠢的事情,请忽略。

我正在尝试检查数据库中的记录,如果电子邮件存在或不通过ajax。我正在尝试为自己使用构建一个小包。我也在使用Vue.js.我在文本框上设置了一个事件onChange(在vue中观看)以检查数据库中是否存在电子邮件。这是我的代码:

创建-tenant.blade.php

<!DOCTYPE html>
<html lang="en">

<head>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<meta name="csrf-token" content="{{ csrf_token() }}">

<title>Admin - Multi Tenancy Solution</title>

<!-- Bootstrap Core CSS -->
<!--<link href="css/bootstrap.min.css" rel="stylesheet"> -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.0.3/vue.js" ></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue-resource/1.0.3/vue-resource.min.js"></script> 

<!-- Custom CSS -->
<style>
body {
    padding-top: 70px;
    /* Required padding for .navbar-fixed-top. Remove if using .navbar-static-top. Change if height of navigation changes. */
}
</style>

<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->

</head>

<body>

<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
    <div class="container">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">Master Portal</a>
        </div>
        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
                <li>
                    <a href="#">About</a>
                </li>
                <li>
                    <a href="#">Services</a>
                </li>
                <li>
                    <a href="#">Contact</a>
                </li>
            </ul>
        </div>
        <!-- /.navbar-collapse -->
    </div>
    <!-- /.container -->
</nav>

<!-- Page Content -->
<div class="container">

    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <h1>Create Tenant</h1>
            <hr>
         <div id="app">
            <form role="form" method="post" action="{{ url('/new-tenant') }}">

                <div class="form-group">
                    <label>Tenant Name:</label><br/>
                    <input type="text" class="form-control" name="tenant_name" required="required" />
                </div>

                <div class="form-group">
                    <label>Tenant Company Name:</label><br/>
                    <input type="text" class="form-control" name="tenant_company_name" required="required" />
                </div>

                <div class="form-group">
                    <label>Tenant Phone:</label><br/>
                    <input type="text" class="form-control" name="tenant_phone"/>
                </div>

                <div class="form-group">
                    <label>Tenant Email:</label><br/>
                    <input type="email" v-model="messageEmail" class="form-control" name="tenant_email" required="required" />
                    <span class="error text-danger text-bold" v-show="!messageEmail">Enter an email</span>
                </div>

                <div class="form-group">
                    <label>Tenant Hostname:</label><br/>
                    <input type="text" class="form-control" name="tenant_hostname" required="required" />
                </div>

                <div class="form-group">
                    <label>Database Name:</label><br/>
                    <input type="text" class="form-control" name="database_name" required="required" />
                </div>



                <div class="form-group">
                    <button type="submit" class="btn btn-primary">Create</button>
                </div>

            </form>
           </div>
        </div>
    </div>
    <!-- /.row -->

</div>
<!-- /.container -->

<!-- jQuery Version 1.11.1 -->
<!--  <script src="js/jquery.js"></script> -->

<!-- Bootstrap Core JavaScript -->
<!-- <script src="js/bootstrap.min.js"></script> -->

<script>
    new Vue({
        el: '#app',
        data: {
            messageEmail: ''
        },

        watch: {
            'messageEmail': function(){
                $.ajax({
                    headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    method: 'POST',
                    url: '/check_email',
                    data: {'tenant_email' : 'tenant_email'},
                    success: function(){
                        alert('hello');
                    }


                });
            }
        } 

    });
</script> 

TenantController.php

 public function check_email(Request $request){
    $input = $request->get('tenant_email');
    $email = TenantModel::where('tenant_email', '=', Input::get('tenant_email'))->first();
    if (is_null($email)) {
       return "email does not exist";
    }else{
      return "email exists";
    }
}

routes.php文件

 <?php

Route::get('tenant', function(){
   echo 'Hello from the multi tenancy package!';
});

Route::get('vue-practise', 'Ashk\Tenancy\TenantController@showvue');

Route::get('add/{a}/{b}', 'Ashk\Tenancy\TenantController@add');
Route::get('sub/{a}/{b}', 'Ashk\Tenancy\TenantController@sub');

Route::get('create-tenant', 'Ashk\Tenancy\TenantController@create');

Route::get('Tenancy/model', function () {
   dd(\Ashk\Tenancy\Models\Item::get());
});

Route::post('/new-tenant', 'Ashk\Tenancy\TenantController@store');

Route::post('check_email', 'Ashk\Tenancy\TenantController@check_email');

我收到错误:

POST http://localhost:8012/check_email 404 (Not Found)
 send @ jquery.min.js:4
 ajax @ jquery.min.js:4
 messageEmail @ create-tenant:146
 run @ vue.js:823
 flushScheduler
 Queue @ vue.js:627
 nextTickHandler @ vue.js:405

请在我做错的地方帮助我。我一直在谷歌上搜索这个问题,但没有任何帮助。

谢谢

0 个答案:

没有答案