Ajax调用,发送图像和其他数据
Ajax calls, sending images and other data
我想知道它有长度限制吗?我将图像和一些数据发送到我的服务器,但它返回无法"找到"所需的数据。有时它可以,有时它找不到。这真的很令人沮丧,因为我找不到问题所在。我将图像作为数据URL发送。服务器什么也得不到。这就像有一个空的formData,但通过检查检查工具,数据写入正确。
还有什么我可以检查的吗?
过帐代码:
var ID = this.id.substr(11);
if (!user) {
if (document.getElementById('userName').value.length < 1) {
Listing.alert('{{trans('adsAdd.enterName')}}', true);
$('#userName').parent().addClass('has-error');
$('#userName').addClass('has-error');
return;
}
if (document.getElementById('userPhone').value < 1) {
Listing.alert('{{trans('adsAdd.enterPhone')}}', true);
$('#userPhone').parent().addClass('has-error');
$('#userPhone').addClass('has-error');
return;
}
if (document.getElementById('userCity').value < 1) {
Listing.alert('{{trans('adsAdd.enterCity')}}', true);
$('#userCity').parent().addClass('has-error');
$('#userCity').addClass('has-error');
return;
}
if (document.getElementById('userEmail').value < 1) {
Listing.alert('{{trans('adsAdd.enterEmail')}}', true);
$('#userEmail').parent().addClass('has-error');
$('#userEmail').addClass('has-error');
return;
}
}
if (document.getElementById('price-' + ID).value.length < 1) {
Listing.alert('{{trans('adsAdd.enterPrice')}}', true);
$('#price-' + ID).parent().addClass('has-error');
$('#price-' + ID).addClass('has-error');
return;
}
if (document.getElementById('description-' + ID).value.length < 1) {
Listing.alert('{{trans('adsAdd.enterDescription')}}', true);
$('#description-' + ID).parent().addClass('has-error');
$('#description-' + ID).addClass('has-error');
return;
}
var formData = new FormData();
var images = [];
$('#img-div-' + ID).find('.img-outer-div').each(function (index, imgElement) {
var img = $(imgElement).find('.item-image');
images.push(img.attr('src'));
});
formData.append('images', JSON.stringify(images));
formData.append('category', document.getElementById('category-' + ID).options[document.getElementById('category-' + ID).selectedIndex].text);
formData.append('price', document.getElementById('price-' + ID).value);
formData.append('description', document.getElementById('description-' + ID).value);
formData.append('time', document.getElementById('time-' + ID).selectedIndex);
formData.append('type', (document.getElementById('type-' + ID) != null) ? document.getElementById('type-' + ID).selectedIndex : 0);
formData.append('lang', '{{App::getLocale()}}');
if (!user) {
formData.append('userName', document.getElementById('userName').value);
formData.append('userPhone', document.getElementById('userPhone').value);
formData.append('userCity', document.getElementById('userCity').value);
formData.append('userEmail', document.getElementById('userEmail').value);
}
formData.append('id', ID);
$(this).attr('disabled', 'true');
var req = new XMLHttpRequest();
req.open('post', '/items/add');
req.send(formData);
req.addEventListener('load', function () {
var obj = $.parseJSON(req.response);
if (obj['message'] != null) {
if (obj['message']['email']) {
Listing.alert(obj['message']['email'], false);
} else if (obj['message']['phone']) {
Listing.alert(obj['message']['phone'], false);
} else if (obj['message']) {
Listing.alert(obj['message'], false);
}
}
var id = obj['id'];
var item = $('#item-' + id);
if (obj['success']) {
item.empty();
item.append('<div class="alert alert-box" id="alert">{{trans('adsAdd.done')}}</div>');
item.fadeOut(4000);
if (!user) {
window.location.replace("{{URL::route('home-index')}}");
}
} else {
item.find('button').prop('disabled', null);
item.find('button').innerHTML = '{{trans('adsAdd.send')}}';
}
});
this.innerHTML = '{{trans('adsAdd.sending')}}';
和php代码:
$message = null; // Message for user
$locale = Input::get('lang');
// Item
$data = array(
'category' => Input::get('category'),
'price' => Input::get('price'),
'description' => Input::get('description'),
'images' => json_decode(Input::get('images')),
'time' => Input::get('time'),
'type' => (Input::get('type') == null) ? 0 : Input::get('type'),
);
$validator = Validator::make($data, array(
'description' => 'required',
'price' => 'required',
'category' => 'required',
'images' => 'required|max:10',
'time' => 'required',
'type' => 'required',
));
if($validator->fails()) {
return Response::json(['success' => false, 'message' => $validator->errors()->first()]);
}
它只到达验证器失败的部分。如果它没有失败,并且数据实际上被发送到服务器,它就会保存它,一切都很好。
如果有帮助,我可以提供一个网页链接,在那里可以查看。
编辑-------------看来有些图片无法发送。单独或与他人合作。有些图片可以单独发送,但不能与其他图片一起发送。一些图片可以单独发送,其他图片也可以发送。这真的很奇怪。
所以真正的答案是@Barmar到底说了什么。我看了看我发送的文件的大小,看起来还可以,但我没有想到的是,我没有把图像作为文件发送,而是作为数据URL发送。这意味着文件大小无关紧要。实际上,我不得不增加实际的帖子大小。
相关文章:
- Ajax调用,发送图像和其他数据
- 在php中的同一网页上检索所选下拉项的值,并使用其他数据将其发布到其他表单
- 基于其他数据结构更新 AngularJS 中的数据结构
- 通过ajax传递数组和其他数据
- Angular Highcharts ng如何将其他数据系列设置为工具提示
- 如何使用xmlhttprequest将图像和其他数据一起发送
- 如何使用RowExpander(正确地)在网格中渲染其他数据
- DHTMLX 网格拉取其他数据
- 如何将外部 API 中的其他数据添加到集合中的现有模型
- 将序列化表单以及一些其他数据发送到控制器
- 将 Javascript 日期解析为 C# DateTime,FF 数据是 Chrome 日期以外的其他数据
- 是否有返回javascript树映射json内部其他数据的方法
- 可以't从窗体将其他数据保存在数据库中
- 将数据模型设置为其他数据模型的属性
- 杜兰达尔.将其他数据传递到其他视图
- 在具有动态x轴类别的Highcharts的工具提示中添加其他数据
- 向Select Option添加属性以提取其他数据
- API不共享URL链接(其他数据很好)
- 从promise的promise中获取数据以及其他数据(promise.all)
- 如何确保某个集合中的数据先于其他数据到达?