douwen5546 2016-11-29 10:59
浏览 76

将JSON数据和文件POST到PHP后端

I've built this custom form where users can click on buttons to configure their own product. This data is then sent with AJAX to my PHP backend (Wordpress) where it is processed and stored in the database. I'd like to allow users to upload files as well, but how can I send this file and the JSON data I have right now with the same POST request to the backend?

configurationData is an array that stores the selected components. detailsFormData contains name, email etc. in the same format.

Javascript that handles the AJAX request:

$.post(
    stac.ajax_url, {
      'action': 'add_configuration',
      'nonce': stac.nonce,
      'configurationData': configurationData,
      'detailsFormData': detailsFormData,
    },
    function( response ) {
      if( response.success ) {
        // do some fancy things
      } else {
        // more stuff
      }
    }
  );

This works fine, but obviously it doesn't let users upload a file yet. I've tried a bunch of tutorials but those only allow a file upload and nothing else, so no json data. Also tried some stuff with var formData = new FormData(this); and adding some settings (processData: false, contentType: false and cache: false), but that posts a single javascript object and I'm not sure how to handle that in the backend.

EDIT: I've seen the links, but they do not explain how I can add this to the data that's being sent: 'action': 'add_configuration', 'nonce': stac.nonce, 'configurationData': configurationData, 'detailsFormData': detailsFormData,

EDIT2: Can just append these in the same way that formData is appended. Should work now, haven't handled it in the backend yet though.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line