【问题标题】:Uncaught TypeError: Illegal invocation error while sending data from ajax未捕获的 TypeError:从 ajax 发送数据时出现非法调用错误
【发布时间】:2016-02-10 03:29:00
【问题描述】:

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

未捕获的类型错误:非法调用

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

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>

【问题讨论】:

  • post_data 是什么?

标签: javascript jquery ajax


【解决方案1】:

由于 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());

【讨论】:

  • 是的 .val() 在我的情况下
猜你喜欢
  • 2016-03-19
  • 2014-08-23
  • 2015-08-13
  • 2019-09-21
  • 2014-06-24
  • 2017-06-16
  • 2013-07-31
  • 2016-12-07
  • 1970-01-01
相关资源
最近更新 更多