我一直试图找到一种方法来确定Laravel中的ajax调用,但我没有找到任何关于它的文档.
我有一个index()函数,我想根据请求的性质不同地处理情况.基本上这是一个绑定到GET请求的资源控制器方法.
public function index()
{
if(!$this->isLogin())
return Redirect::to('login');
if(isAjax()) // This is what i am needing.
{
return $JSON;
}
$data = array();
$data['records'] = $this->table->fetchAll();
$this->setLayout(compact('data'));
}
Run Code Online (Sandbox Code Playgroud)
我知道在PHP中确定Ajax请求的其他方法,但我想要一些特定于Laravel的东西.
谢谢
更新:
我试过用
if(Request::ajax())
{
echo 'Ajax';
}
Run Code Online (Sandbox Code Playgroud)
但我收到错误: Non-static method Illuminate\Http\Request::ajax() should not be called statically, assuming $this from incompatible context
该类显示这不是静态方法.
我需要通过ajax添加新对象,但我不知道如何在laravel中使用$ .ajax()函数.
我在刀片模板中的形式是:
{{Form::open(array('url'=>'expense/add', 'method' => 'POST', 'class' => 'form-signin', 'id' => 'expenseForm'), array('role'=>'form'))}}
{{Form::select('period_id', $data['period'], null, array('class' => 'form-control'))}}
{{Form::select('expense_category_id', $data['categories'], null, array('class' => 'form-control'))}}
{{Form::text('date_expense', null, array('placeholder' => 'Fecha', 'class' => 'form-control'))}}
{{Form::text('amount', null, array('placeholder' => '¿cuanto fue?', 'class' => 'form-control'))}}
{{Form::hidden('user_id', Auth::user()->id)}}
<br />
{{Form::button('Add expense', array('class'=>'btn btn-lg btn-primary btn-block', 'id' => 'btnSubmit'))}}
{{Form::close()}}
Run Code Online (Sandbox Code Playgroud)
我在控制器中的代码是:
public function addExpense(){
$expense = new Expense;
$data = Input::all();
if ($expense->isValid($data)) {
$expense->fill($data);
$expense->save();
//Recargar la tabla gastos
return Redirect::to('expense/index')->with('success', 'El …Run Code Online (Sandbox Code Playgroud) 使用这个问题的代码,
@extends('layouts.' . isset($ajax) ? 'ajax' : 'master')
Run Code Online (Sandbox Code Playgroud)
检查Ajax.它适用于常规Ajax页面加载,但不适用于使用弹出窗口.
在这种情况下我使用弹出Magnific酒店的Ajax模式中,请求头是XMLHttpRequest的但Laravel返回非Ajax(扩展)的布局.