gpt4 book ai didi

javascript - 使用 PHP 将特定数据从 angularJS 应用程序发送到 json 文件

转载 作者:行者123 更新时间:2023-12-03 08:32:05 24 4
gpt4 key购买 nike

我有一个 AngularJS 库应用程序和一个 JSON 数据文件,其中包含这样的书籍信息数组

[
{
"name" : "test1",
"pages": 0,
"author": "author1",
"year": 1940,
"section": "history",
"current": 0,
"description": "bla bla bla",
"bookUrl": "data/book.pdf"
},
{
"name" : "test1",
"pages": 0,
"author": "author1",
"year": 1940,
"section": "history",
"current": 0,
"description": "bla bla bla",
"bookUrl": "data/book.pdf"
}
]

“当前”是我正在阅读的当前书籍的当前页
阅读 View 中有“下一页”和“上一页”按钮当我按“下一步”时,它会将“+1”添加到“当前”页数问题是(如何使用PHP将此+1发送到JSON文件中的“当前”?)我有这个 PHP 代码:

<?php
$jsonString = file_get_contents('library.json');
$data = json_decode($jsonString, true);
$data[0]['current'] = 3;
$newJsonString = json_encode($data);
file_put_contents('library.json', $newJsonString);
?>

看到 ($data[0]) 是第一本书的索引,我如何将当前书籍的索引发送到 PHP,以便它更新当前书籍的“当前”数据? 这是“下一个”功能:

scope.goNext = function() {
if (scope.pageToDisplay >= pdfDoc.numPages) {
return;
}
scope.pageNum = parseInt(scope.pageNum) + 1;
$http.get("data/insert.php")
.success(function(data, status, headers, config) {
console.log("data inserted successfully");
});
};

这是读取 Controller :

 app.controller('read', ['$scope','books', '$routeParams',function($scope,books, $routeParams) {
books.success(function(data){
$scope.book = data[$routeParams.bookId]
$scope.pdfUrl = data[$routeParams.bookId].bookUrl;
$scope.pdfName = data[$routeParams.bookId].name;
});
//the pdf viewer options
//$scope.pdfUrl = 'data/tohfa12.pdf';
$scope.scroll = 0;
$scope.loading = 'Loading file please wait';
$scope.getNavStyle = function(scroll) {
if(scroll > 100) {
return 'pdf-controls fixed';
} else {
return 'pdf-controls';
}
};
$scope.onError = function(error) {
console.log(error);
};
$scope.onLoad = function() {
$scope.loading = '';
};
$scope.onProgress = function(progress) {
console.log(progress);
};
$scope.currentBookIndex = parseInt($routeParams.bookId);
}]);

我知道这很复杂,但我真的需要它,谢谢。

最佳答案

您希望您的应用程序表现如何?

您需要随请求一起发送 bookIdpageNum!

scope.goNext = function() {
if (scope.pageToDisplay >= pdfDoc.numPages) {
return;
}
scope.pageNum = parseInt(scope.pageNum) + 1;
$http.get("data/insert.php", {
param : {
bookId: $scope.bookId,
pageNum: $scope.pageNum
}
})
.success(function(data, status, headers, config) {
console.log("data inserted successfully");
});
};
顺便说一句。根据 REST 设计,GET http 请求永远不应该更改资源。 GET 用于阅读。如果您想更新资源,您应该使用 POST、PUT 或 DELETE

关于javascript - 使用 PHP 将特定数据从 angularJS 应用程序发送到 json 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33296968/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com