Пустой ответ от поста с formData - PullRequest
0 голосов
/ 26 апреля 2018

В 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 голосов
/ 26 апреля 2018

Я думаю, что проблема может быть с типом заголовка контента, он должен

{ Content-type : 'application/json' }

А также попробуйте отправить данные с

body :JSON.stringify(data)

Надеюсь, это поможет

0 голосов
/ 26 апреля 2018

Вы вызываете then после fetch's then, которого не существует.

Вы можете использовать функции стрелок для доступа к переменной ответа, полученной из запроса

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) => {
  // now you can access response here
  console.log(response)
})
...