我正在尝试使用axios检索数组来执行ajax请求,但结果始终为空。有趣的是同样的请求与artisan tinker
完全一致!
让我们看一些代码:
route / web.php文件
Route::get('/trocaCarros/{id}', 'VitrineController@trocaCarros')->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);
}
浏览器响应是:
{}
但是,当我在修补匠那里做到了:
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个人
有谁知道我错过了什么?
答案 0 :(得分:1)
你必须从控制器的url中获取param。 更新你的VitrineController.php就像打击一样。
public function trocaCarros($id, Request $request)
{
$marca = $id;
$umaMarcaModelo = Modelo::where('marca_id', $marca)->get();
return response()->json($umaMarcaModelo);
}
答案 1 :(得分:0)
当axios时,您可以使用以下方式获取数据:
axios.post(url,params)
.then(function(response) {
// response.data contains the data
}).catch(function(error) {
});