【发布时间】:2017-06-04 03:01:53
【问题描述】:
我这里有问题。我正在开发我的 Web 应用程序,我希望它通过 ajax 连接到我的 API。我正在尝试通过 ajax 从客户端(即网络)的表单将图像发送到我的 api。
所以,这是我在客户端的表单..
{{ Form::open(['enctype' => "multipart/form-data", 'id' => 'addplant', 'method' => 'POST', 'files' => true, 'class' => 'col s12']) }}
{{ csrf_field() }}
<div class="row" style="margin-top:10%;">
<div class="col s12 center">
<img class="circle" id="image_url" src=""></a>
{!! Form::file('image_url', array('id' => 'image', 'class' => 'form-control')) !!}
</div>
</div>
<div class="row">
<div class="input-field col s6">
{{ Form::text('herbal_name', null, array('id' => 'herbal_name', 'class' => 'form-control validate')) }}
{{ Form::label('herbal_name', 'Herbal Name') }}
</div>
<div class="input-field col s6">
{{ Form::text('scientific_name', null, array('id' => 'scientific_name', 'class' => 'form-control validate')) }}
{{ Form::label('scientific_name', 'Scientific Name') }}
</div>
</div>
{{ Form::submit('Add plant', array('class' => 'btn btn-primary right add')) }}
{{ Form::close() }}
这是我的 ajax,仍在客户端
<script type="text/javascript">
$(".add").click(function() {
$.ajax({
url: 'http://127.0.0.1/identificare_api/public/api/plants',
data: new FormData($("#addplant")[0]),
type: "POST",
success: function( msg ) {
console.log(msg);
},
error: function(){
alert('pangit');
}
});
});
</script>
编辑:在我的 api 中,我只有这个
return json_encode($request->file('image_url'));
我在这里缺少什么?我错过了什么吗?
更新:我尝试应用@bfcior 的答案,但是当我尝试console.log(base64img) 时,它会返回这个非常长的字符串,并且比您预期的要长。
【问题讨论】:
标签: php jquery ajax forms laravel-5.3