jquery 使用文件和发布数据来验证请求

332nm8kg  于 2023-11-17  发布在  jQuery
关注(0)|答案(1)|浏览(89)

我允许自己张贴,因为我不能发送我的邮件请求,其中包含一个数据文件和两个职位
验证码:

$('#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正确发送数据

rwqw0loc

rwqw0loc1#

你不能把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)
  })
}

字符串

相关问题