admin管理员组文章数量:1399217
I have an ajax function that posts an image, which works perfectly in Chrome and Firefox. Safari and iOS Safari both balk at it, though.
I'm creating and appending the value like this:
var ajaxImage = new FormData();
ajaxImage.append('file-0', $('.some-file-input')[0].files[0]);
I then call this image later, using ajaxImage.entries()
to init the iterator for the FormData
object, so that I can perform a validation on it. However, in Safari ajaxImage.entries()
throws an entries is not a function
TypeError.
I guess I could just do the validation before getting to this point as a workaround, but now it's bugging me so I wanted to see if anyone could shed some light on this.
Thanks!
I have an ajax function that posts an image, which works perfectly in Chrome and Firefox. Safari and iOS Safari both balk at it, though.
I'm creating and appending the value like this:
var ajaxImage = new FormData();
ajaxImage.append('file-0', $('.some-file-input')[0].files[0]);
I then call this image later, using ajaxImage.entries()
to init the iterator for the FormData
object, so that I can perform a validation on it. However, in Safari ajaxImage.entries()
throws an entries is not a function
TypeError.
I guess I could just do the validation before getting to this point as a workaround, but now it's bugging me so I wanted to see if anyone could shed some light on this.
Thanks!
Share Improve this question asked Dec 7, 2016 at 22:47 Jonathan BowmanJonathan Bowman 1,6461 gold badge12 silver badges17 bronze badges2 Answers
Reset to default 5Unfortunately, Safari doesn't support this part of the specification: https://developer.mozilla/en-US/docs/Web/API/FormData#Browser_patibility, specifically the entries method.
I haven't tried it myself, but perhaps a polyfill like this one: https://github./francois2metz/html5-formdata might work?
But yes, you might be right -- doing validation before that point might be worth it.
I solved this by conditionally (if Safari is the browser) iterating through the elements property of the form. For all other browser, my wrapper just iterates through FormData entries(). The end result of my function, in either case, is a simple javascript object (JSON) which amounts to name/value pairs. That object can be passed directly to the data property of the JQuery ajax function (with contentType and processData not specified).
function FormDataNameValuePairs(FormName)
{
var FormDaytaObject={};
var FormElement=$('#'+FormName).get(0);
if (IsSafariBrowser())
{
var FormElementCollection=FormElement.elements;
//console.log('namedItem='+FormElementCollection.namedItem('KEY'));
var JQEle,EleType;
for (ele=0; (ele < FormElementCollection.length); ele++)
{
JQEle=$(FormElementCollection.item(ele));
EleType=JQEle.attr('type');
// https://github./jimmywarting/FormData/blob/master/FormData.js
if ((! JQEle.attr('name')) ||
(((EleType == 'checkbox') || (EleType == 'radio')) &&
(! JQEle.prop('checked'))))
continue;
FormDaytaObject[JQEle.attr('name')]=JQEle.val();
}
}
else
{
var FormDayta=new FormData(FormElement);
for (var fld of FormDayta.entries())
FormDaytaObject[fld[0]]=fld[1];
}
return FormDaytaObject;
}
where IsSafariBrowser() is implemented by whatever your favorite method is, but I chose this:
function IsSafariBrowser()
{
var VendorName=window.navigator.vendor;
return ((VendorName.indexOf('Apple') > -1) &&
(window.navigator.userAgent.indexOf('Safari') > -1));
}
Example usage with an ajax call:
var FormDaytaObject=FormDataNameValuePairs('FiltersForm');
$.ajax({url: 'AJAXDoSomethingWithFilters/',
method: 'POST',
data: FormDaytaObject,
dataType: 'text',
success: function(data)
{
console.log('AJAXDoSomethingWithFilters success:'+data);
},
error: function(JQXhr,Status,Err)
{
console.log('AJAXDoSomethingWithFilters error:'+Err);
}
});
本文标签: javascriptSafari is saying FormDataentries() is not a functionStack Overflow
版权声明:本文标题:javascript - Safari is saying FormData.entries() is not a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744206017a2595191.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论