php Laravel - 检查是否有 Ajax 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29231587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel - check if Ajax request
提问by Raheel
I have been trying to find a way to determine ajax call in Laravel but i don't find any document regarding it.
我一直试图找到一种方法来确定 Laravel 中的 ajax 调用,但我没有找到任何关于它的文档。
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.
我有一个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'));
}
I know the other methods of determining the Ajax request in PHP but i want something specific to Laravel.
我知道在 PHP 中确定 Ajax 请求的其他方法,但我想要一些特定于 Laravel 的方法。
Thanks
谢谢
Updated:
更新:
I tried using
我尝试使用
if(Request::ajax())
{
echo 'Ajax';
}
But i am receiving error : Non-static method Illuminate\Http\Request::ajax() should not be called statically, assuming $this from incompatible context
但我收到错误: 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.
该类表明这不是静态方法。
回答by Crack_David
Maybe this helps. You have to refer the @param
也许这有帮助。你必须参考@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";
}
回答by Jnanaranjan
To check an ajax request you can use if (Request::ajax())
要检查您可以使用的 ajax 请求 if (Request::ajax())
Note: If you are using laravel 5, then in the controller replace
注意:如果您使用的是 Laravel 5,则在控制器中替换
use Illuminate\Http\Request;
with
和
use Request;
I hope it'll work.
我希望它会起作用。
回答by lukasgeiter
You are using the wrong Request
class. If you want to use the Facade like: Request::ajax()
you have to import this class:
您使用了错误的Request
类。如果你想像这样使用 Facade:Request::ajax()
你必须导入这个类:
use Illuminate\Support\Facades\Request;
And not Illumiante\Http\Request
并不是 Illumiante\Http\Request
Another solution would be injecting an instance of the real request class:
另一种解决方案是注入真实请求类的实例:
public function index(Request $request){
if($request->ajax()){
return "AJAX";
}
(Now here you have to import Illuminate\Http\Request
)
(现在你必须导入Illuminate\Http\Request
)
回答by Илья Зеленько
$request->wantsJson()
$request->wantsJson()
You can try $request->wantsJson()
if $request->ajax()
does not work
$request->wantsJson()
如果$request->ajax()
不起作用,您可以尝试
$request->ajax()
works if your JavaScript library sets an X-Requested-With HTTP header.
$request->ajax()
如果您的 JavaScript 库设置了 X-Requested-With HTTP 标头,则有效。
By default Laravel set this header in js/bootstrap.js
默认情况下,Laravel 在 js/bootstrap.js 中设置这个头文件
window.axios = require('axios');
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
In my case, I used a different frontend code and I had to put this header manually for $request->ajax()
to work.
在我的例子中,我使用了不同的前端代码,我不得不手动放置这个标题$request->ajax()
才能工作。
But $request->wantsJson()
will check the axios query without the need for a header X-Requested-With
:
但是$request->wantsJson()
会检查 axios 查询而不需要标题X-Requested-With
:
// Determine if the current request is asking for JSON. This checks Content-Type equals application/json.
$request->wantsJson()
// or
\Request::wantsJson() // not \Illuminate\Http\Request
回答by Matthew Way
if(Request::ajax())
Looks to be the right answer. http://laravel.com/api/5.0/Illuminate/Http/Request.html#method_ajax
看起来是正确的答案。 http://laravel.com/api/5.0/Illuminate/Http/Request.html#method_ajax
回答by Justin
For those working with AngularJSfront-end, it does not use the Ajax header laravel is expecting. (Read more)
对于那些使用AngularJS前端的人来说,它不使用 laravel 期望的 Ajax 标头。(阅读更多)
Use Request::wantsJson()for AngularJS:
对 AngularJS使用Request::wantsJson():
if(Request::wantsJson()) {
// Client wants JSON returned
}
回答by lee shin
Those who prefer to use laravel helpers they can check if a request is ajax using laravel request()
helper.
那些喜欢使用 laravel helper 的人可以使用 laravel helper 检查请求是否是 ajax request()
。
if(request()->ajax())
// code
回答by Nagibaba
Sometimes Request::ajax()
doesn't work, then use \Request::ajax()
有时Request::ajax()
不起作用,然后使用\Request::ajax()
回答by Nagibaba
public function index()
{
if(!$this->isLogin())
return Redirect::to('login');
if(Request::ajax()) // This is check ajax request
{
return $JSON;
}
$data = array();
$data['records'] = $this->table->fetchAll();
$this->setLayout(compact('data'));
}
回答by Muhammad Awais
after writing the jquery code perform this validation in your route or in controller.
编写 jquery 代码后,在您的路由或控制器中执行此验证。
$.ajax({
url: "/id/edit",
data:
name:name,
method:'get',
success:function(data){
console.log(data);}
});
Route::get('/', function(){
if(Request::ajax()){
return 'it's ajax request';}
});