weixin_33711641 2015-04-07 10:28 采纳率: 0%
浏览 5

追加表格数据对象?

Im sending an image to the server using form data.

//create form data from form
var formData = new FormData($('#my-form')[0]);

//append the image
formData.append('file', myFile);

//add data from crop plugin
formData.append('file-data', cropData);

//POST
$.ajax({
        url: /upload,
        type: 'POST',
        dataType: 'json',
        processData: false,
        contentType: false,
        data: formData
    })

My issue occurs when I try and add the data from the crop plug in, it logs ok:

Object {x: 140, y: 273.49999999999994, width: 520, height: 520, rotate: 0}

but once posted in PHP it just dumps as an empty array with no properties.

Where am I going wrong?

  • 写回答

2条回答 默认 最新

  • weixin_33704234 2015-04-07 10:34
    关注
    var temp = Object {x: 140, y: 273.49999999999994, width: 520, height: 520, rotate: 0}
    
    JSON.stringify(temp) 
    // that will be ok looks in php in json_decode($_POST['data'])   
    
    $.ajax({
      url: '/upload',
      type: 'POST',
      dataType: 'json',
      processData: false,
      contentType: false,
      data: JSON.stringify(temp)
    })
    

    and in php:

    $data = json_decode( $_POST['data'] ) ;
    

    Thats the answer - you have to convert object/hash from js to json before send to php.

    评论

报告相同问题?