doukezi4576 2018-05-15 09:26
浏览 54
已采纳

$ _POST从ajax req为空

so I'm trying to upload files via a form sent using AJAX, except $_POST returns an empty array (so does $_FILES) - though I'm not sure why. Here is the top of my form:

HTML - generated from PHP (inside WP function)

$html .= '<form method="post" class="form-horizontal" id="product-slides-form" enctype="multipart/form-data">';

AJAX

//edit product gallery
$('#update-product-gallery').on('click', function()
{
    var product_id       = $(this).data('id'),
        slides_form      = $('#product-slides-form'),             
        slides_form_data = new FormData(slides_form[0]);

    //create array of slides
    var slides  = {},
        counter = 0;

    $.each(slides_form.find('input'), function(j, v)
    {
        if ($(this)[0].files) {
            $.each($(this)[0].files, function(i, files)
            {
                slides_form_data.append('slides-'+ counter, files);
                counter++;
            })
        }
    });

    //add slideshow data
    slides_form_data.append('slides', JSON.stringify(slides));
    slides_form_data.append('product-id', product_id);

    var slides_data = {};
    slides_data['product_id']  = product_id;

    slides_form_data.forEach(function(val, key)
    {
        slides_data[key] = val
    });

    //if I change data: to below test FormData than it works
    var test = new FormData();
    test.append('me', 1);

    $.ajax({
        data:        slides_data,
        dataType:    'text',
        type:        'post',
        url:         PLUGIN_ROOT+ 'admin/inc/scripts/add-product-slides.php',
        cache:       false,
        contentType: false,
        processData: false,
        success:     function(res) {console.log(res)},
        error:       function(res) {$.fn.showPopup(2, 'Something went wrong. Please try again.', res)}
    })
});

and my script just var_dumps $_POST + $_FILES for now

I console.log'd the FormData using this:

// Display the key/value pairs
for (var pair of slides_data.entries()) {
    console.log(pair[0]+ ', ' + pair[1]); 
}

and it returns the correct data, so I'm really not sure why my script doesn't get the $_POST data? Am I missing something really obvious?

(if more code is needed - comment and I'll add more)

  • 写回答

1条回答 默认 最新

  • doushu7588 2018-05-15 09:34
    关注
    var slides_data = {};
    

    That is not a FormData object. You need to send a FormData object.

    You create one here:

    slides_form_data = new FormData(slides_form[0]);
    

    Keep using slides_form_data for all your data. Don't create a second variable and ignore all the work you did to populate the first one.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于温度改变石墨烯介电性能(关键词-介电常数)
  • ¥150 HDMI分路器LT86102 的输出在890MHz频点处EMC超标8DB
  • ¥15 druid(相关搜索:数据库|防火墙)
  • ¥15 大一python作业
  • ¥15 preLaunchTask"C/C++: aarch64- apple-darwin22-g++-14 生成活动 文件”已终止,退出代码为-1。
  • ¥60 如何鉴定微信小程序数据被篡改过
  • ¥18 关于#贝叶斯概率#的问题:这篇文章中利用em算法求出了对数似然值作为概率表参数,然后进行概率表计算,这个概率表是怎样计算的呀
  • ¥20 C#上传XML格式数据
  • ¥15 elementui上传结合oss接口断点续传,现在只差停止上传和继续上传,各大精英看下
  • ¥20 手机截图相片分辨率降低一半