Laravel-检查是否有Ajax请求 [英] Laravel - check if Ajax request
问题描述
我一直试图在Laravel中找到一种确定ajax调用的方法,但是我没有找到任何有关它的文档.
I have been trying to find a way to determine ajax call in Laravel but i don't find any document regarding it.
我有一个index()
函数,我想根据请求的性质以不同的方式处理情况.基本上,这是绑定到GET请求的资源控制器方法.
I have a index()
function which i want to handle situation differently based on the nature of request. Basically this is a resource controller method that is bound to GET request.
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'));
}
我知道用PHP确定Ajax请求的其他方法,但我想要Laravel特有的东西.
I know the other methods of determining the Ajax request in PHP but i want something specific to Laravel.
谢谢
已更新:
我尝试使用
if(Request::ajax())
{
echo 'Ajax';
}
但是我收到错误消息:Non-static method Illuminate\Http\Request::ajax() should not be called statically, assuming $this from incompatible context
But i am receiving error : Non-static method Illuminate\Http\Request::ajax() should not be called statically, assuming $this from incompatible context
该类表明这不是静态方法.
The class shows that this is not a static method.
推荐答案
也许有帮助.您必须引用@param
Maybe this helps. You have to refer the @param
/**
* Display a listing of the resource.
*
* @param Illuminate\Http\Request $request
* @return Response
*/
public function index(Request $request)
{
if($request->ajax()){
return "AJAX";
}
return "HTTP";
}
这篇关于Laravel-检查是否有Ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!