gpt4 book ai didi

ajax - 如何在ajax和laravel中传递数组和其他字段

转载 作者:行者123 更新时间:2023-12-04 10:50:24 25 4
gpt4 key购买 nike

我正在使用 ajax 和 laravel 上传多个文件。我将文件名存储在 MySQL 数据库中。在 Controller 中,我正在检查是否正在获取数组。只有 else 块在工作。当我提交数据时,它没有传递数组。我不知道为什么。请检查我的代码,我错了。

谢谢

 <form  enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group">

<div class="row text-center">
<div class="col-lg-3">
Select Event
</div>
<div class="col-lg-6">
<select name="eventname" class="form-control">
<option disabled selected>Select</option>
@foreach($event as $eve)
<option value="{{$eve->id}}">{{$eve->title}}</option>
@endforeach
</select>
</div>

</div>
</div>

<div class="form-group">

<div class="row text-center">
<div class="col-lg-3">
Upload A file
</div>
<div class="col-lg-6">
<input type="file" id="eventgallery" class="form-control" name="eventgallery[]" multiple>
</div>

</div>

</div>
<div class="form-group">
<div id="images">

</div>
</div>
</form>

Ajax 代码
$('#eventgallery').on('change',function(){
var image = '';
$.ajax({
url : '{!! url('uploadimage') !!}',
date: new FormData(this.form),
method:'POST',
dataType: 'json',
contentType:false,
cache:false,
processData:false,
success:function(data)
{
console.log(data);
}

});

});

Controller
 public function store(Request $request)
{
$name = $request->file('eventgallery');

if (is_array($name) || is_object($name))
{
foreach($name as $file)
{
$extension = $file->getClientOriginalExtension();
$filename = asset('uploads/eventgallery'.$request->eventname.rand(11111,9991).'.'.$extension);
$file->move("uploads/eventgallery",$filename);

$event = EventGallery::create([
'event_name' => $request->eventname,
'eventimage_url' => $filename
]);
}
$eve = Event::find($request->eventname);
$eventgallery = $eve->galleryies;
return response()->json($eventgallery);
}
else
{
return response()->json('it is not a array');

}



}

最佳答案

问题是您将帖子变量发送为 date .应该是 data .

$('#eventgallery').on('change',function(){
var image = '';
$.ajax({
url : '{!! url('uploadimage') !!}',
data: new FormData(this.form),
method:'POST',
dataType: 'json',
contentType:false,
cache:false,
processData:false,
success:function(data)
{
console.log(data);
}

});

});

关于ajax - 如何在ajax和laravel中传递数组和其他字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59508303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com