提问人:seb31 提问时间:11/16/2023 最后编辑:seb31 更新时间:11/16/2023 访问量:18
带有文件和 POST 数据的 AJAX 请求
ajax request with a file and post data
问:
我允许自己发帖,因为我无法发送包含一个数据文件和两个帖子的 ajax 请求
法典:
$('#image--1').change(function (e) {
e.preventDefault();
var img = new FormData();
img.append( 'media', $('#image--1')[0].files[0]);
var idarticle = $('#article').val();
uploadphoto(img,1,idarticle)
});
function uploadphoto(media,numphoto,idarticle)
{
console.log('num : ' + numphoto);
console.log('idarticle : ' + idarticle);
$.ajax({
type: "POST",
url: "ajax/change-photo.php",
processData: false,
contentType: false,
data: {
media:media,
numphoto:numphoto,
idarticle:idarticle
},
dataType: "JSON"
}).done(function (ret){
console.log(ret.responseText)
}).fail(function(ret){
console.log(ret.responseText)
})
}
控制台上的返回:
<b>Warning</b>: Undefined array key "idarticle" in <b>/srv/web/gestionnel/public/ajax/change-photo.php</b> on line <b>7</b><br />
NULL
<br />
<b>Warning</b>: Undefined variable $id_article in <b>/srv/web/gestionnel/public/ajax/change-photo.php</b> on line <b>13</b><br />
我想使用 Ajax 正确发送我的数据
答:
1赞
Barmar
11/16/2023
#1
你不要放在对象里面,它必须是整个值。您应该向其添加其他参数。media
data:
function uploadphoto(media, numphoto, idarticle) {
console.log('num : ' + numphoto);
console.log('idarticle : ' + idarticle);
media.append('numphoto', numphoto);
media.append('idarticle', idarticle);
$.ajax({
type: "POST",
url: "ajax/change-photo.php",
processData: false,
contentType: false,
data: media,
dataType: "JSON"
}).done(function(ret) {
console.log(ret.responseText)
}).fail(function(ret) {
console.log(ret.responseText)
})
}
评论