我正在使用laravel和ajax上传图片。以下是我的HTML代码:
<form id='uploadImageForm' class='uploadImageForm'><div class='form-actions noborder'><input type='file' class='file' name='file' attr='file'><input type='hidden' class='productVariantId' name='productVariantId' value='21'><input type='hidden' value='{{ Session::token() }}' class='_token' name='_token'><input type='submit' class='btn default' value='Upload'><label for='multiple' class='control-label'>Add Product Variant Images</label></div></form><br>
这是我的ajax请求代码:
$(document).on('submit', '.uploadImageForm', function (event) {
event.preventDefault();
var data = new FormData(this);
for (var value of data.values()) {
console.log(value);
}
var request = new XMLHttpRequest();
request.open("POST", "{{ route('productVariantImages.add') }}");
request.send(data);
});
目前,在将formData对象发送到laravel路由后,令牌变量在浏览器的网络部分中可见。以下是截图: