【问题标题】:How to send form-data as a JSON Object..?如何将表单数据作为 JSON 对象发送..?
【发布时间】:2021-06-13 00:27:51
【问题描述】:

我的服务器端有一些文本输入和图像。和 我需要将此数据作为 JSON 对象发送。但是由于 FormData,我无法发送这样的图像。所以我需要将我的表单数据转换为一个 JSON 对象。 请帮我... 谢谢你..!

HTML 部分 -

    <form>
        <div class="form-group">
            <label for="txtcustomerImage"> <i class="tags icon"></i> Image Of Your NIC</label>
            <input class="form-control-file" id="txtcustomerImage" type="file" name="txtcustomerImage">
            </div>
    </form>

Ajax 部分 -

$('#btnCreateNewAccount').click(function () {


    var fileObject = $("#txtcustomerImage")[0].files[0];//access file object from input field
    var fileName = $("#txtcustomerImage")[0].files[0].name; //get file name
    var form = new FormData(); //setup form data object to send file data
    form.append("custImage", fileObject, fileName); //append data



    console.log('clicked..');
    let customerNIC = $('txtcustomerNIC').val();
    let customerName = $('txtcustomerName').val();
    let customerAddress = $('txtcustomerAddress').val();

    console.log(form)

    $.ajax({
        method: "post",
        url: "http://localhost:8080/Sprinf_Final-Back-end/customer",
        contentType: "application/json",
        data: JSON.stringify({
            customerNIC: customerNIC,
            customerName: customerName,
            customerAddress: customerAddress,

        }),
        success: function (res) {
            if (res.massage == 'Success') {
                alert('Your Account is Successfully Created!When You Log to Server Use Your User Name & Password..!');
                console.log(res);
            } else {
                console.log('error');
            }
        }
    });

});

【问题讨论】:

  • 吹毛求疵:这是JSON object。这可能不是你想要的。

标签: javascript jquery ajax spring spring-boot


【解决方案1】:

如果您想以 JSON 格式发送数据,则创建 FormData 对象是没有意义的。可以通过fileObject.text()读取文件内容并发送:

$('#btnCreateNewAccount').click(async function() {
  const fileObject = $("#txtcustomerImage")[0].files[0]; //access file object from input field
  const fileName = fileObject.name; //get file name
  const data = JSON.stringify({
      fileObject: await fileObject.text(),
      fileName
  })
  console.log(data);  
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
  <div class="form-group">
    <label for="txtcustomerImage"> <i class="tags icon"></i> Image Of Your NIC</label>
    <input class="form-control-file" id="txtcustomerImage" type="file" name="txtcustomerImage">
  </div>
</form>
<button id="btnCreateNewAccount">
Click
</button>

【讨论】:

  • 我可以将 const 数据保存在 mn SQL DB 中吗?
  • @Gathsara 数据在客户端代码中是 const 的。发送到服务器后没有区别。 JSON 中没有 const。
  • private byte [] custImage - 这可以存储图像数据..?
  • @Gathsara 是的,一个字节数组可以存储图像数据。
  • CustomerDTO(custID=null, custName=null, custAddress=null, custImage=null) -- 但我的值为 null..我不理解
猜你喜欢
  • 2017-03-12
  • 1970-01-01
  • 2019-05-06
  • 2013-02-09
  • 1970-01-01
  • 2017-10-01
  • 1970-01-01
  • 2014-04-07
  • 2019-06-21
相关资源
最近更新 更多