Laravel 5.1 AJAX不工作? [英] Laravel 5.1 ajax not working?

查看:137
本文介绍了Laravel 5.1 AJAX不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图提交laravel使用AJAX的登录表单,但它似乎是AJAX的部分不工作。下面是我的login.blade.php

i am trying to submit login form using ajax in laravel but it seems to be ajax part not working. below is my login.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> 

</head>
<body>


<div class="secure">Secure Login form</div>
{!! Form::open(array('url'=>'account/login','method'=>'POST', 'id'=>'myform')) !!}
<div class="control-group">
  <div class="controls">
     {!! Form::text('email','',array('id'=>'','class'=>'form-control span6','placeholder' => 'Email')) !!}
  </div>
</div>
<div class="control-group">
  <div class="controls">
  {!! Form::password('password',array('class'=>'form-control span6', 'placeholder' => 'Please Enter your Password')) !!}
  </div>
</div>
{!! Form::button('Login', array('class'=>'send-btn')) !!}
{!! Form::close() !!}



<script type="text/javascript">
$(document).ready(function(){
  $('.send-btn').click(function(){ 
$val=$('input[name=email]').val();  
$token=$('input[name=_token]').val();
//alert($val+$token);
   $.ajax({
      url: '/account/login',
      type: "post",

      data: {'email':$('input[name=email]').val(), '_token': $('input[name=_token]').val()},
      success: function(data){
        alert(data);
      }
    });  


  }); 
});
</script>

route.php

route.php

Route::get('account/login', function() {
  return View::make('login');
});
Route::post('account/login', 'AccountController@login');

在控制器

 public function login() {

    if(Request::ajax()) {
      $data = Input::all();
      print_r($data);

      die;
    }

    }

我试图在成功之前,阿贾克斯的一部分提醒输入值,它会打印输出,但成功的警报不会工作。

I have tried to alert input values in ajax part before success, it will print output but success alert wont work.

推荐答案

虽然通过AJAX记录的用户来说是不安全的,这是可以做到无 - - 减:

Though logging in users through AJAX is insecure, it can be done none-the-less:

{!! Form::open(array('url'=>'account/login','method'=>'POST', 'id'=>'myform')) !!}
    /**
     * Your Form
     */
    ...
    {!! Form::submit('Login', array('class'=>'send-btn')) !!}
{!! Form::close() !!}

在CSRF字段将自动为您生成。 JavaScript的部分:

The CSRF field will be automatically generated for you. The JavaScript part:

<script>
    (function($) {
        $('#myform').submit(function(e) {
            $.ajax({
                type: 'POST',
                url: 'account/login',
                data: $('#myform').serialize(),
                success: function(data) {
                    alert(data);
                }
            });

            e.preventDefault();
        });            
    })(window.jQuery);
</script>

这是应该做的伎俩为HTML和JavaScript的一部分。在Laravel 5.1,我们可以利用方法注入到接收输入的字段(请求对象)像这样:

That should do the trick for the HTML and JavaScript part. In Laravel 5.1 we can leverage Method Injection to receive the Input fields (Request Object) like so:

<?php

namespace Blah\Blah;

use Illuminate\Http\Request;

class AccountController
{
    ...

    public function login(Request $request)
    {
        if ($request->ajax()) {
            // Its AJAX!!
            return $request->all(); // All the Input fields
        }
    }
}

请注意,我做你的code中的变化。请检查该方法是否工作。

Notice the changes that I did to your code. Please check whether this method works or not.

干杯!

这篇关于Laravel 5.1 AJAX不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆