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.

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

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?