laravel - Laravel 6 表单数组 $request->input() 不起作用
问题描述
我有一系列价格,如下所示:
<input type="input" id="prices[type][1]" name="prices[type][1]">
<input type="input" id="prices[type][2]" name="prices[type][2]">
我通过发布请求(JSON:是的,Content-Type
设置为application/json
)发送此数据,并希望在我使用时获得一个数组,$request->input('prices')
但这并没有真正发生。也试过了$request->get('prices')
。
当我这样做时,$request->all()
我确实得到了我提交的所有数据:
JS 用来发出请求:
const response = await fetch(this.action, {
method: 'POST',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': this.$page.token,
},
body: this.formData(),
});
const body = await response.json();
this.formData()
:
formData(): Object {
const formData = new FormData(this.$el);
return JSON.stringify(Array.from(formData.entries()).reduce((memo, pair) => ({
...memo,
[pair[0]]: pair[1],
}), {}));
},
有没有人知道哪里可能出错?
解决方案
嗯,即使您这样做,看起来数组也已损坏,all()
因为我没有看到type
数组中的键。
试试这个:
dd(json_decode($request->getContent(), true));
由于它是 JSON,因此您需要获取正文并将其转换为数组。
推荐阅读
- angular - 离子和科尔多瓦错误离子科尔多瓦准备ios
- c++ - 为什么我需要使这个非朋友 == 运算符重载成员函数以避免“未定义引用”错误?
- javascript - react canvas.scale 在设置时似乎行为不正确
- html - 使用 highlight.js CDN
- python - 为什么没有从数据框中删除所有异常值?
- ios - UIColorPickerViewController 拖动关闭未触发
- typescript - 你可以将类型别名传递给 TypeScript 中的接口吗?
- javascript - 维护被阻止的用户列表并过滤被阻止的用户
- c++ - Visual Studio 2015 构建工具/CPP 运行时 v140 Dockerfile 无法正常工作
- python - 解码 Base64 字符串时填充不正确