English 中文(简体)
将各种物体应用于反应堆中的表格Data
原标题:Appending an array of objects to FormData in React

我知道这个问题有十几次,但我似乎无法得到任何工作答案。

我有一份反射前线和DjangoResend,我试图在请求中发送一系列物体。 我恳切地在邮政局工作,既包括JSON的原始投入,也包括该表。

“entergraph

“entergraph

So I know that my backend should be able to handle it fine, but no matter what I try in my React code I get the same 400 error. Here is my (slightly trimmed) function from my front end:

    const formData = new FormData();
    if(data.photo.length >= 1) {
        formData.append( photo , data.photo[0]);
    }
    formData.append( recipe_name , data.recipe_name);

    formData.append( ingredients , JSON.stringify(data.ingredients));
    
    // data.ingredients.forEach(element => {
    //     console.log(JSON.stringify(element))
    //     formData.append( ingredients[] , JSON.stringify(element))
    // });

    // for(let i=0; i < data.ingredients.length; i++) {
    //     console.log(data.ingredients[i])
    //     formData.append( ingredients[] , JSON.stringify(data.ingredients[i]))
    // }

    console.log(formData)

    const config = {
        headers: {
             content-type :  multipart/form-data ,
             Authorization : localStorage.getItem( access_token ) ?
             Bearer   + localStorage.getItem( access_token ) :
            null,
        },
    };
    axiosInstance.post( /recipes/ , formData, config).then((response) => {
        console.log(response.data);
        navigate( /recipe/  + response.data.id);
    });

I left some commented out code of different things I ve tried, but to summarize, I ve tried:

formData.append( ingredients , data.ingredients);

formData.append( ingredients , JSON.stringify(data.ingredients));

formData.append( ingredients[] , JSON.stringify(data.ingredients));

data.ingredients.forEach(element => {
    formData.append( ingredients[] , JSON.stringify(element))
});

data.ingredients.forEach(element => {
    formData.append( ingredients , JSON.stringify(element))
});

data.ingredients.forEach(element => {
    formData.append( ingredients , element)
});

data.ingredients.forEach(element => {
    formData.append( ingredients[] , element)
});

我甚至试图用同样的调子来做传统的通路,但却没有成功。

我也试图调查我从前端寄来的数据本身,但只要我能告诉他们,他们也没有错过。 这里是来自我表格组成部分的原始数据:

“在此处的影像描述”/</a

以及个人要素(每 lo中印):

“在此处的影像描述”/</a

和通过JSON.string()通过的同样内容

“enter像

我真的是在试图做什么时丧生,我感到像我这样基本上尝试了一切,我以前在座各位发现的同一问题的所有答案都表明我所尝试的东西。 任何帮助都将受到高度赞赏。

问题回答

我认为,你必须把它作为“json案”补充:

var jsonObject = {
  key1:  value1 ,
  key2:  value2 ,
  key3:  value3 
};

var jsonBlob = new Blob([JSON.stringify(jsonObject)], { type:  application/json  });

formData.append( jsonFile , jsonBlob,  data.json );




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签