Jquery 和 HTML FormData 返回“未捕获的类型错误:非法调用”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12755945/
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
Jquery and HTML FormData returns "Uncaught TypeError: Illegal invocation"
提问by Caio Tarifa
I'm using this script to upload my image files: http://jsfiddle.net/eHmSr/
我正在使用此脚本上传我的图像文件:http: //jsfiddle.net/eHmSr/
$('.uploader input:file').on('change', function() {
$this = $(this);
$('.alert').remove();
$.each($this[0].files, function(key, file) {
$('.files').append('<li>' + file.name + '</li>');
data = new FormData();
data.append(file.name, file);
$.ajax({
url: $('.uploader').attr('action'),
type: 'POST',
dataType: 'json',
data: data
});
});
});
But when I click in upload button, the JavaScript console returns this error:
但是当我单击上传按钮时,JavaScript 控制台返回此错误:
Uncaught TypeError: Illegal invocation
Can you help me?
你能帮助我吗?
回答by Blender
jQuery processes the data
attribute and converts the values into strings.
jQuery 处理data
属性并将值转换为字符串。
Adding processData: false
to your options object fixes the error, but I'm not sure if it fixes the problem.
添加processData: false
到您的选项对象可以修复错误,但我不确定它是否可以解决问题。
回答by Hamid
i had same problem i fixed that with using two options
我有同样的问题,我用两个选项解决了这个问题
contentType: false
processData: false
Actually I Added these two command to my $.ajax({}) function
实际上我将这两个命令添加到我的 $.ajax({}) 函数中
回答by Anup Shetty
Adding processData: false
to the $.ajax
options will fix this issue.
添加processData: false
到$.ajax
选项将解决此问题。
回答by Nowdeen
My experience:
我的经验:
var text = $('#myInputField');
var myObj = {title: 'Some title', content: text};
$.post(myUrl, myObj, callback);
The problem is that I forgot to add .val() to the end of $('#myInputField'); this action makes me waste time trying to figure out what was wrong, causing Illegal Invocation Error, since $('#myInputField') was in a different file than that system pointed out incorrect code. Hope this answer help fellows in the same mistake to avoid to loose time.
问题是我忘记在 $('#myInputField'); 的末尾添加 .val(); 此操作让我浪费时间试图找出错误所在,从而导致 Illegal Invocation Error,因为 $('#myInputField') 与该系统指出的错误代码位于不同的文件中。希望这个答案可以帮助犯同样错误的人避免浪费时间。
回答by Deepak Srivastav
In my case, there was a mistake in the list of the parameters was not well formed. So make sure the parameters are well formed. For e.g. correct format of parameters
就我而言,参数列表中有一个错误,格式不正确。因此,请确保参数格式正确。例如,正确的参数格式
data: {'reporter': reporter,'partner': partner,'product': product}