javascript - 未捕获的类型错误 : Illegal invocation error while sending data from ajax

标签 javascript jquery ajax

我有一个表单,我试图通过 ajax 发送值。但我收到以下错误:

Uncaught TypeError: Illegal invocation

我卡住了,找不到我哪里错了。有什么建议吗?

Ajax

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" </script>
<script type="text/javascript">
$(document).ready(function() {
$("#submit_custom").click(function() {
 e.preventDefault();
var postData = new FormData();
  postData.append('file[]', $('input[type=file]')[0].files[0]); // after this line
  postData.append('trxid', $('input[name=trxid]'));
  postData.append('orderid', $('input[name=orderid]'));
  postData.append('custid', $('input[name=custid]'));
  if(proceed)

 $.post('upload.php', post_data, function(response){
 if(response.type == 'error'){ //load json data from server and output message
  output = '<div class="error">'+response.text+'</div>';
  }else{
  output = '<div class="success">'+response.text+'</div>';
  $("#upload_form").slideUp(); //hide form after success
                    }
  $("#upload_form").hide().html(output).slideDown();
  }, 'json');
  });
  });
  });
    </script>
</head>
<body>
<h2>Hello <?php echo $user ?> </h2> <p> "You have successfully done purchasing process.</p>
<div id="upload_form">
<p>To send your size details for your order please upload the following file:</p>
<p>Download custom size form Provide us your custom size: <a  href="download.php?download_file=custom-measurement-form.pdf">File.pdf</a></p>
<form enctype="multipart/form-data" action="" method="post">
    <input type="text" name="trxid" value="<?=$trx?>">
    <input type="text" name="orderid" value="<?=$orderid?>">
    <input type="text" name="custid" value="<?=$customerid?>">
    <input type="file" name="file">
    <input type="submit" id="submit_custom">
</form>
</div>
</body>
</html>

最佳答案

因为 jQuery 无法为 $.post 序列化 postData 对象,所以您得到了非法调用。

我认为您需要添加 .val()

postData.append('file[]', $('input[type=file]')[0].files[0].val()); // after this line
postData.append('trxid', $('input[name=trxid]').val());
postData.append('orderid', $('input[name=orderid]').val());
postData.append('custid', $('input[name=custid]').val());

关于javascript - 未捕获的类型错误 : Illegal invocation error while sending data from ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35300334/

相关文章:

javascript - 以新标签打开弹出窗口

javascript - Putty 窗口关闭时,机器人将关闭

javascript - 基于多个 CSS 类的排序,以及使用 Jquery 设计代码

javascript - 从javascript获取视频源的问题

javascript - 如何通过javascript为标签赋值?

php - 如何在 Ajax 和 PHP 中创建自动完成搜索?

javascript - 为什么我可以成功 AJAX Get 请求 fonts.googleapis.com (而不是 cnn.com)?

javascript - 隐藏/显示 ngFor 内的单个或特定按钮

如果对象的其他值相同,则 JavaScript SUM 对象值

python - WSGI:使用 AJAX 从 python 脚本中获取字符串