我正在尝试在 laravel 中使用 ajax 从数据库获取数据。我获取了除图像之外的所有数据并将其成功加载到编辑表单中。我从数据库中获取了图像,但无法在编辑表单上预览它。
这是我的ajax调用
function editProduct(id, category_id) {
$.ajax({
url: "{{ url('admin/products/edit/') }}/" + id,
type: "GET",
contentType: false,
cache: false,
processData: false,
dataType: "json",
success: function(response) {
$("#edit-modal").modal("toggle");
$(".id").val(response.id);
$("#edit_name").val(response.name);
$("#edit_category").val(category_id);
$("#edi_image").val(); <---What to do here? --->
}
});
}
这是我的控制器代码
public function edit($id) {
$product = FoodItem::findOrFail($id);
return response()->json($product);
}
我想在这里加载图片
<img src='' id="edit_image" width="180px;" height="120" class="mt-2">
<p>Old image</p>
谁能告诉我该怎么做?
我希望您将图像路径存储在数据库中,并将图像存储在公共资产中。如果是这种情况,那么只需使用 jQuery 更新图像 src
$("#edi_image").attr('src','images/'+response.image); // change the path to your image
非常感谢。谢谢先生。 非常感谢...谢谢..非常感谢.....