这是我需要在ajax中做出回应的函数
function load_id_user(id) {
var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
alert(id);
$.ajax({
url: 'load-user',
type: 'POST',
data: {_token: CSRF_TOKEN,id:id},
success: function (data) {
$( "#user_fields" ).html( data );
}
});
}
这是我的路线。
Route::post('load-user','UserController@load_id');
这是我的控制器
public function load_id() {
$var=DB::select('select * from user where user_id="$_POST[id]"');
//$var=Usuarios::select()->where("user_id","=",$_POST['id'])->get();
foreach ($var as $user) {
echo '<input type="text" name="cedula" value="'.$user->user_id.'" onblur="load_id_user(this.value);" placeholder="Cedula" class="form-control" required>';
}
}
当我尝试运行此操作时,控制台说出错误500,尝试所有操作并且没有任何作用。
答案 0 :(得分:0)
尝试将控制器代码更改为
public function load_id (Request $request) {
$id = $request->input('id');
$response = '';
$var = Usuarios::where("user_id", "=", $id)->get();
foreach ($var as $user) {
$response .= '<input type="text" name="cedula" value="'.$user->user_id.'" onblur="load_id_user(this.value);" placeholder="Cedula" class="form-control" required>';
}
return $response;
}
和你的ajax请求
$.ajax({
url: window.location.origin + '/load-user',
type: 'POST',
data: {
_token: CSRF_TOKEN,
id:id
},
success: function (data) {
$( "#user_fields" ).html( data );
}
});