[英]Json file not getting updated when using post method in javascript?
This is the js code for post:这是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));
}
This is the backend code:这是后端代码:
public function getInvoice(Request $request){
$invoice = json_encode($request->all());
$result = file_put_contents(base_path('storage/app/invoice.json'),$invoice);
}
Each time while posting, it shows only one entry.每次发布时,它只显示一个条目。 ie The content is overridden by another instead of creating new entries?即内容被另一个覆盖而不是创建新条目?
{
userId: 1,
Total: 3333
}
Try this for appending to a file instead of just overwriting it:试试这个附加到文件而不是仅仅覆盖它:
I've set the append flag and added a lock to the operation.我设置了 append 标志并为操作添加了锁。 Read more here .在这里阅读更多。
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.