javascript - 在javascript中使用post方法时Json文件没有得到更新?
问题描述
这是post的js代码:
async function invoice() {
const baseurl = 'http://127.0.0.1:8000/api/getInvoice/';
let response = await fetch(baseurl, {
method: 'POST',
body: JSON.stringify({
userId: 1,
Total: 3333
}),
headers: {
'Content-type': 'application/json; charset=UTF-8',
},
})
.then((response) => response.json())
.then((json) => console.log(json));
}
这是后端代码:
public function getInvoice(Request $request){
$invoice = json_encode($request->all());
$result = file_put_contents(base_path('storage/app/invoice.json'),$invoice);
}
每次发布时,它只显示一个条目。即内容被另一个覆盖而不是创建新条目?
{
userId: 1,
Total: 3333
}
解决方案
试试这个附加到文件而不是仅仅覆盖它:
我已经设置了附加标志并为操作添加了一个锁。在这里阅读更多。
public function getInvoice(Request $request){
$invoice = json_encode($request->all());
$result = file_put_contents(base_path('storage/app/invoice.json'), $invoice, FILE_APPEND | LOCK_EX);
}
推荐阅读
- c# - RichTextBox 分配新行但仅显示最后一行
- javascript - 渲染器问题中的电子非上下文感知本机模块
- javascript - TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator))
- html - 使用 mix-blend-mode 从其父级中剔除一个形状,同时保持同级文本正常
- javascript - 如何将“CLEAR”按钮添加到 IonDateTime?
- python - 从阈值蒙版生成圆形粒子的分割蒙版?
- c++ - 如何将字符串映射到对象成员?
- haskell - 评估部分应用的功能
- append - 如何使用通用模式在 Nuxt 中附加 html
- json - Pandas read_json - 停止将索引转换为日期时间