2017-10-17 1 views
0

브라질 인사입니다! Axis를 사용하여 배열을 검색하여 Ajax 요청을 수행하려고하는데 결과가 항상 비어 있습니다. 재밌는 것은 똑같은 요청이 숙련공 팅커으로 잘 작동한다는 것입니다! 의 일부 코드를 보자 :AXIOS가 아약스 요청시 빈 배열을 반환합니다.

경로/web.php 파일을

Route::get('/trocaCarros/{id}', '[email protected]')->name('trocaCarros'); 

Vuejs 방법 :

methods: { 
umaMarca() { 
// alert(marca.value); 
axios.get('/trocaCarros/' + marca.value).then(response => this.vitrine = response.data); 
    } 

VitrineController.php

public function trocaCarros(Request $request) 
{ 
$marca = $request->id; 
$umaMarcaModelo = Modelo::where('marca_id','=', $marca); 
return response()->json($umaMarcaModelo); 
} 

브라우저 응답은 다음과 같습니다

나는 어설프게에서 그것을했다 63,210
{} 

그러나, 그런 다음

Psy Shell v0.8.11 (PHP 7.1.6 — cli) by Justin Hileman 
$marca = 212 
=> 212 

...

$modelo = App\Modelo::where('marca_id','=', $marca)->get(); 

나는 데이터베이스에있는 것처럼 같이, (6 개) 결과를 반환 :

=> Illuminate\Database\Eloquent\Collection {#994 
all: [ 
    App\Modelo {#995 
    id: 880011, 
    descricao: "Accord", 
    marca_id: "212", 
    categoria_id: "1", 
    ativo: 1, 
    created_at: "2017-08-03 18:35:19", 
    updated_at: "2017-08-03 19:41:36", 
    }, 
    App\Modelo {#996 
    id: 880012, 
    descricao: "City", 
    marca_id: "212", 
    categoria_id: "1", 
    ativo: 1, 
    created_at: "2017-08-03 18:47:26", 
    updated_at: "2017-08-03 18:47:26", 
    }, 

... 외 4 명

아무도 내가 놓친 것을 아는가? 감사합니다.

+1

는 잊어'->()는'처음 얻을? – kerbholz

+0

예, 잊어 버렸습니다. Thakns –

답변

1

컨트롤러의 URL에서 매개 변수를 가져와야합니다. 당신의 The VitrineController.php를 한 발 날처럼 업데이트하십시오. Axios의, 당신은 사용하여 데이터를 얻을 수있을 때

public function trocaCarros($id, Request $request) 
{ 
    $marca = $id; 
    $umaMarcaModelo = Modelo::where('marca_id', $marca)->get(); 
    return response()->json($umaMarcaModelo); 
} 
+0

내가 그랬다고 믿을 수 없어! LOL !!!! 고마워요 @ 모센 –

+0

좋아, 이제는 DIV에 결과를 렌더링하고 싶습니다. jQuery를 사용하여이를 수행하는 방법을 알고 있지만 AXIOS가이를 처리하는 방법은 무엇입니까? –

0

는 :

axios.post(url,params) 
.then(function(response) { 
    // response.data contains the data 
}).catch(function(error) { 

}); 
관련 문제