繁体   English   中英

为什么使用FormData进行此jQuery POST调用以上传文件而不成功进行调用?

[英]Why is this jQuery POST call, using FormData, to upload a file, not successfully making the call?

调试此简单文件时遇到问题,无法测试我的上传页面是否正常工作。 在我看来,该代码看起来正确,但是运行不正确。

 <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("#button_a").click(function(){ var formData = new FormData($(this)[0]); $.post("./../pages/upload_image.php", formData, function(data) { $("#content").html(data); }); return false; }); }); </script> </head> <body> <h2>Test upload_image.php</h2> <form onsubmit="return false" method="POST" enctype="multipart/form-data"> <input id="file" name="file" type="file" placeholder="File..."> <input type="hidden" name="MAX_FILE_SIZE" value="1000000" /> <br /> <button id="button_a">Upload</button> </br> </br> <h3>Output:</h3> <div id="content" style="padding: 50px; background-color: #CCC;"> </div> </body> </html> 

快速浏览显示您正在使用

var formData = new FormData($(this)[0]);

尝试将其更改为:

var formData = new FormData($('form')[0]);

我认为您的$(this)上下文是Anchor标记,而不是表单。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM