laravel - Laravel API 调用表单验证不起作用
问题描述
我正在使用 Laravel API 并以两种不同的方式调用 API 方法。在单向表单验证中工作,但在第二种表单验证中不起作用。但我真的需要 API 调用的第二种方式。
saveMember: function () {
let that = this;
let formData = new FormData();
formData.append('member_info', that.member_info);
// member_info is a Json Object
// This is First way , form validation working good while calling Api
axios.post('/api/member/save_member', that.member_info)
// This is second way, form validation not working
axios.post('/api/member/save_member', that.formData)
.then(function (response) {
console.log("response Here: " + response.data.success);
that.errors = response.data.success;
// location.reload(true);
})
.catch(function (error) {
that.errors = error.response.data.errors;
console.log("Error Here: " + error.response.data);
});
}
在 Laravel 请求控制器中进行表单验证。
解决方案
Look like Json Object can send to API. I missing your error when request call to fixed.
But i think you need convert FormData to Json Object:
var obj = {};
that.formData.forEach(function(val, idx){
obj[idx] = val;
});
var json = JSON.stringify(obj);
and then resend:
axios.post('/api/member/save_member', json)
...
推荐阅读
- python - 如何将文本添加到 python 列表中的每个其他项目?
- symfony - Symfony Messenger / RabbitMQ 中的消费者错误处理
- filter - 根据计数和存储值的组合计算百分比 - Power BI
- multithreading - 如何构建在球拍 lisp 引擎中使用的程序?
- python - Pandas 将两行合二为一
- c# - 当相机透视像 Matlab 视图一样发生变化时,修改 WPF 中绘制的轴上的 3D 坐标
- kubernetes - 无法将 kubernetes-bionic main (Ubuntu 18.04) 添加到 apt 存储库
- php - Doctrine AnnotationRegistry registerLoader替换
- angularjs - AngularJS - 使用'track by $index'时下拉过滤不会更新
- wordpress-gutenberg - 如何使用 php $content 在 Gutenberg 版本 4 中渲染 InnerBlocks