admin管理员组文章数量:1355648
I'm testing out sending a formData object to PHP (I am following /), but am having some difficulty getting it off the ground. First, the formData object is created and populated with:
var formdata = new FormData();
formdata.append('my_key','my_value');
Then my ajax call with jQuery is:
$.ajax({
url: 'php_upload.php',
type: 'POST',
cache: false,
data: formdata,
processData: false,
contentType: false,
success: function (response) {
console.log(response);
}
});
With the php_upload.php file containing:
<?php
echo $_FILES['my_key']['name'];
?>
But I get an undefined index: my_key error in the console.
Anyone have any idea what I might be doing wrong? Been scratching my head for ages.
I'm testing out sending a formData object to PHP (I am following http://net.tutsplus./tutorials/javascript-ajax/uploading-files-with-ajax/), but am having some difficulty getting it off the ground. First, the formData object is created and populated with:
var formdata = new FormData();
formdata.append('my_key','my_value');
Then my ajax call with jQuery is:
$.ajax({
url: 'php_upload.php',
type: 'POST',
cache: false,
data: formdata,
processData: false,
contentType: false,
success: function (response) {
console.log(response);
}
});
With the php_upload.php file containing:
<?php
echo $_FILES['my_key']['name'];
?>
But I get an undefined index: my_key error in the console.
Anyone have any idea what I might be doing wrong? Been scratching my head for ages.
Share Improve this question edited Jan 6, 2013 at 22:51 Ali Ben Messaoud 11.9k8 gold badges57 silver badges88 bronze badges asked Jan 6, 2013 at 22:34 xdlxdl 1,1102 gold badges14 silver badges22 bronze badges1 Answer
Reset to default 8You haven't added any files to the FormData, just a string which can be accessed by $_POST['my_key']
.
To pass a file the second parameter of FormData.append has to be a FILE
or a BLOB
.
本文标签: javascriptPHP not receiving formData object from ajaxStack Overflow
版权声明:本文标题:javascript - PHP not receiving formData object from ajax - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744054444a2583016.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论