Laravel-Ajax 요청 여부 확인
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'));
}
PHP에서 Ajax 요청을 결정하는 다른 방법을 알고 있지만 Laravel에 특정한 것을 원합니다.
감사
업데이트 :
나는 사용해 보았다
if(Request::ajax())
{
echo 'Ajax';
}
하지만 오류가 발생합니다. Non-static method Illuminate\Http\Request::ajax() should not be called statically, assuming $this from incompatible context
클래스는 이것이 정적 메서드가 아님을 보여줍니다.
아마도 이것이 도움이 될 것입니다. @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";
}
ajax 요청을 확인하려면 다음을 사용할 수 있습니다. if (Request::ajax())
참고 : laravel 5를 사용하는 경우 컨트롤러에서
use Illuminate\Http\Request;
와
use Request;
나는 그것이 효과가 있기를 바랍니다.
잘못된 Request
클래스를 사용하고 있습니다. Facade를 다음과 같이 사용하려면 Request::ajax()
이 클래스를 가져와야합니다.
use Illuminate\Support\Facades\Request;
그리고 Illumiante\Http\Request
또 다른 해결책은 실제 요청 클래스의 인스턴스를 주입하는 것입니다.
public function index(Request $request){
if($request->ajax()){
return "AJAX";
}
(이제 여기에서 가져와야합니다 Illuminate\Http\Request
)
$ request-> wantsJson ()
작동하지 않으면 시도 $request->wantsJson()
할 수 있습니다.$request->ajax()
$request->ajax()
JavaScript 라이브러리가 X-Requested-With HTTP 헤더를 설정하면 작동합니다.
기본적으로 Laravel은 js / bootstrap.js에이 헤더를 설정합니다.
window.axios = require('axios');
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
제 경우에는 다른 프런트 엔드 코드를 사용했고이 헤더를 수동으로 넣어야 $request->ajax()
작동했습니다.
그러나 $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
if(Request::ajax())
정답 인 것 같습니다. http://laravel.com/api/5.0/Illuminate/Http/Request.html#method_ajax
AngularJS 프론트 엔드로 작업하는 사람들을 위해 laravel이 기대하는 Ajax 헤더를 사용하지 않습니다. ( 더 읽기 )
Use Request::wantsJson() for AngularJS:
if(Request::wantsJson()) {
// Client wants JSON returned
}
Those who prefer to use laravel helpers they can check if a request is ajax using laravel request()
helper.
if(request()->ajax())
// code
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'));
}
Sometimes Request::ajax()
doesn't work, then use \Request::ajax()
after writing the jquery code perform this validation in your route or in controller.
$.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';}
});
참고URL : https://stackoverflow.com/questions/29231587/laravel-check-if-ajax-request
'your programing' 카테고리의 다른 글
msbuild를 사용하여 모든 파일 및 폴더 복사 (0) | 2020.10.06 |
---|---|
django.core.exceptions.ImproperlyConfigured : MySQLdb 모듈로드 오류 : MySQLdb라는 모듈이 없습니다. (0) | 2020.10.06 |
Android에서 USB 디버깅을 활성화하는 방법은 무엇입니까? (0) | 2020.10.06 |
파일 또는 어셈블리 'Antlr3.Runtime (1)'또는 해당 종속성 중 하나를로드 할 수 없습니다. (0) | 2020.10.06 |
React는 로컬 이미지를로드하지 않습니다. (0) | 2020.10.06 |