jQuery 'FormData' 仅在 IE 中未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19486597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'FormData' is undefined in IE only
提问by Roy M J
I have a problem where i need to post the data as content-type application/x-www-form-urlencoded
.
我有一个问题,我需要将数据发布为 content-type application/x-www-form-urlencoded
。
var inputData = {cId:"444",pageNo:"1",latitude:"49.153236",longitude:"12.040905"};
var data = new FormData();
data.append('data', JSON.stringify(inputData));
this.model.save(data, {
data: data,
processData: false,
cache: false,
contentType: false,
success: function (model, resultData) {
$.get(App.baseUrl + 'templates/all-offers-view.html', function (data) {
template = _.template(data, {
data: resultData
});
that.$el.html(template);
}, 'html');
},
error: function (error) {
console.log("Error");
return false;
}
});
While the above works fine in all other browsers, I am getting the following error in IE9.
虽然以上在所有其他浏览器中都可以正常工作,但我在 IE9 中收到以下错误。
SCRIPT5009: 'FormData' is undefined
view.js, line 57 character 9
line 57 being var data = new FormData();
第 57 行是 var data = new FormData();
Ive heard FormData()
is a browser dependant function and its not related to jquery library and that in IE its missing.
我听说FormData()
是一个依赖于浏览器的函数,它与 jquery 库无关,而在 IE 中它丢失了。
The reason why i am using the above method is because i have to pass data in application/x-www-form-urlencoded
format.
我使用上述方法的原因是因为我必须以application/x-www-form-urlencoded
格式传递数据。
I cannot change the server side coding(as this is linked with an iphone app in appstore).
我无法更改服务器端编码(因为它与 appstore 中的 iphone 应用程序相关联)。
All i can do is try out with the client-side.
我所能做的就是在客户端尝试。
Does anyone have a solution for this?
有没有人对此有解决方案?
p.s : I am using backbone.js.
ps:我正在使用backbone.js。
回答by harsh4u
Try below code:
试试下面的代码:
if(typeof FormData == "undefined"){
var data = [];
data.push('data', JSON.stringify(inputData));
}
else{
var data = new FormData();
data.append('data', JSON.stringify(inputData));
}
Hope this help you
希望这对你有帮助