使用formData从帖子中清空响应

时间:2018-04-26 12:41:14

标签: laravel vue.js fetch form-data

在VueJS中我正在尝试发布帖子

let data = new FormData()
data.append('name', 'hey')

fetch('http://homestead.test/api/customers', {
    method: 'POST',
    headers: {
        'Content-type': 'multipart/form-data'
    },
    body: data
})
.then((response) => response.json())
.then((response) => {
    console.log(response)
})

添加了资源路径

Route::resource('customers', 'CustomerController');

并返回请求

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    return $request->all();
}

我的console.log打印以下内容

[]

2 个答案:

答案 0 :(得分:0)

你在fetch之后调用then然后不存在。

您可以使用箭头功能来访问您从请求中获得的响应变量

<ion-slides class="imageSlider"  pager>
        <ion-slide [(ngModel)]="imagePaths" *ngFor="let imagePath of imagePaths | async, let i = index">
                <img class="img_card" src="{{ imagePath }}" />
        </ion-slide>
            <ion-slide (click)="addImages()">
                    <ion-icon name="add"></ion-icon>
                    <p>Voeg foto's toe van uw woning</p>
                </ion-slide>
    </ion-slides>

答案 1 :(得分:0)

我认为问题可能在于它应该

的内容标题类型

{ Content-type : 'application/json' }

并尝试使用

发送数据

body :JSON.stringify(data)

我希望它会有所帮助

相关问题