[英]Receiving 'data is not defined' error in console when doing a POST query in AJAX
[英]options.data.indexOf is not a function when doing an ajax POST
我收到此错误: Uncaught TypeError: options.data.indexOf is not a function
。
我一直在寻找解决方案,我发现这可能是因为 jQuery 版本(我有 jQuery 3.3.1 顺便说一句)。 但大多数问题都是微不足道的,比如$(window).on('load', ...)
而不是$(window).load(...)
。 对于这个,我不知道实际发生了什么。
所以这里是代码:
function changePhotoPOST() {
var form = document.getElementById('someFormName');
var formData = new FormData(form);
$.ajax({
type: "POST",
url: "...someUrl...",
data: formData,
processData: false,
contentType: false,
success: onSuccess,
error: onFailed
});
}
表格如下所示:
<form id="someFormName">
<input type="text" value="@User.Identity.Name" name="Email" hidden />
<input id="file" type="file" onchange="changePhotoPOST()" name="File" hidden />
<a class="some classes" onclick="document.getElementById('file').click(); return false">Some text</a>
</form>
我使用样式链接打开文件选择器,并在选择文件后立即提交表单。 尽管如此,表单也可能是具有可见输入的普通表单,我认为这并不重要。
So when the file is picked the the form is submitted, I get the mentioned error. 据我了解 indexOf 出于某种原因用于“数据”? 某处是否存在语法错误? 我可以在不更改 jQuery 版本的情况下解决这个问题吗?
使用表单数据,例如:
function changePhotoPOST() {
var form = $("form").serialize();
var formData = new FormData(form);
$.ajax({
type: "POST",
url: "...someUrl...",
data: formData,
processData: false,
contentType: false,
success: onSuccess,
error: onFailed
});
}
不确定您是否粘贴了您正在使用的确切代码,但您的元素 ID 是错误的。
这个小提琴似乎有效(我没有接收数据但发送请求的 url)。
HTML:
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
</head>
<body>
<form id="ChangePhotoForm">
<input type="text" value="asdsa" name="Email" />
<input id="file" type="file" onchange="changePhotoPOST()" name="File" hidden />
<a class="some classes" onclick="document.getElementById('file').click(); return false">Some text</a>
</form>
</body>
</html>
JS:
function changePhotoPOST() {
var form = document.getElementById("ChangePhotoForm");
var formData = new FormData(form);
$.ajax({
type: "POST",
url: "myUrl",
data: formData,
processData: false,
contentType: false,
success: onSuccess,
error: onFailed
});
}
function onSuccess(response) {
console.log("response", response);
}
function onFailed(response) {
console.log("response", response);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.