weixin_33690963 2012-12-09 21:49 采纳率: 0%
浏览 39

使用ajax发送图像和文本

I'm using javascript's 'formData' to send image files with ajax. How can I attach other data to formData, for example a text string?

JS:

$("#post-image").click(function(){
    $.ajax({
        url: "../../build/ajaxes/upload-photo.php",
        type: "POST",
        data: formdata,
        processData: false,
        contentType: false,
        success: function (response) {
            console.log(response);
        }
    });
});

PHP:

In my PHP I can access the image by using $_FILES["images"]. I don't know what to use for my additional data passed with formdata.

  • 写回答

3条回答 默认 最新

  • weixin_33705053 2012-12-09 21:51
    关注

    You need to submit the form as multipart. Here's a SO solution to your question: Making an HTTP POST call with multipart/form-data using jQuery?

    Not an exact duplicate, so I didn't mark it as such, but it's a direct reference to the solution to your problem.

    Edit: you'll notice that some of the answers reference FormData as a solution as well. Once you get the data posting properly, you can then access it with the normal $_FORM collection in PHP.

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100