我尝试从sweetalert输入中获取列表的名称,并使用axios将其传递给CategoryController,以便使用add_list()函数将其添加到数据库中
侧值:
const { add_list } = useList()
const listName=ref()
function addList() {
const inputValue = ''
Swal.fire({
title: 'Add List',
input: 'text',
inputLabel: 'Enter the list name',
inputValue: inputValue,
showCancelButton: true,
background: '#fefbe9',
confirmButtonColor: '#007E33',
allowOutsideClick: false,
inputValidator: (value) => {
if (!value) {
return 'You need to write something!'
}
}
}).then((result) => {
if (result.isConfirmed) {
listName.value = result.value
add_list(listName.value)
}
})
}
列表. js:
const add_list = async (data) => {
let formData = new FormData();
formData.append('name', data);
axios.post('/api/dashboard/addlist', formData).then(response => {
console.log(response)
if (response.data.success) {
Swal.fire({
title: 'Success!',
text: response.data.success,
icon: 'success',
confirmButtonText: 'Ok',
confirmButtonColor: '#007E33',
background: '#fef6dd',
allowOutsideClick: false,
})
}else{
alert('err')
}
}).catch(function (error) {
console.log(error);
})
}
这里axios不发送CategoryController中的formData。
分类控制器. php:
public function add_list(Request $request)
{
$request->validate([
'name' => 'required'
]);
$category = $request->all();
Category::create($category);
return response()->json(['success' => "The list '" . $request->name . "' successfully created"]);
}
最后,没有向“类别”表添加任何列表。
请帮帮我!
1条答案
按热度按时间nzk0hqpo1#
我相信,由于您正在发布formData,而axios假定使用json,因此您可能必须传入一个头,告诉服务器您正在发布的是formdata。
这可能会有帮助。