我想用javascript将laravel id分配到formData中

vwoqyblh  于 2022-11-18  发布在  Java
关注(0)|答案(1)|浏览(107)

我试图添加一个'common_id'到我的laravel项目,但我使用的是formData而不是html表单,所以值字段不起作用,有什么想法我可以插入这个:

<input type="hidden" name="common_id" value="{{$common->id}}">

变成这样:

$("#done").click(function(){
      let upload_image = mainCanvas.toDataURL('image/png', 98)
      // console.log("Image URL:", upload_image)

      let one_file = urlToFile(upload_image)
      uploadOne(one_file)
    });

    let urlToFile = (url) => {
      let arr = url.split(",")
      // console.log(arr)
      let mime = arr[0].match(/:(.*?);/)[1]
      let data = arr[1]

      let dataStr = atob(data)
      let n = dataStr.length
      let dataArr = new Uint8Array(n)

      while(n--){
        dataArr[n] = dataStr.charCodeAt(n)
      }

      let file = new File([dataArr], 'First.png', {type: mime})

      return file
    }

    let uploadOne = (file) => {
      const url = "http://127.0.0.1:8000/store"
      
      const payload = new FormData()
      // payload.append('file', file)
      payload.append('file', file)

      fetch(url, {
        method: "POST",
        body: payload,
        processData: false,
        contentType: false, 
        headers: {
          'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
      })
    }

没有html输入标签,只使用表单数据
我已经尝试使用另一个append标记

const commonId = "$common->id"
      
      const payload = new FormData()
      // payload.append('file', file)
      payload.append('file', file)
      payload.append('hidden', common_id, commonId);

但它不起作用

zvokhttg

zvokhttg1#

您应该像这样使用公共id:

const commonId = "{{ $common->id }}"

相关问题