【发布时间】:2017-06-16 07:47:38
【问题描述】:
我正在使用 AngularJS 开发应用程序。但我仍然不太清楚 POST、PUT 和 GET 的区别。当服务器端不需要任何前端数据将数据返回到客户端时,我通常使用 $http GET 从服务器获取数据,如下所示。
$http.get(configSettings.baseUrl +"retrive_user.php").success(function (data) {
}).error(function() {
console.log("error");
});
当我使用 POST 时,服务器端需要前端数据才能将数据返回到客户端,如下所示。
$http({
url: configSettings.baseUrl + "insert_message.php",
method: "POST",
data: {
'username': username,
'messageContent' : messsageContent,
'sender_id': usernameID,
'subscribeChannel' : subscribeChannel,
'attachmentType' : attachmentType,
'event' : 'chat_message'
}
}).success(function(response) {
console.log(response);
}).error(function(response) {
console.log(response);
})
});
即使,我想在我的 MySQL 数据库中删除数据或编辑数据,我在 angularjs 中使用 POST 方法,然后在我的 PHP 服务器端,我喜欢跟随来获取数据。
$chat_info = file_get_contents("php://input");
$chat_request = json_decode($chat_info,true);
@$username = $chat_request['username'];
@$messageContent = $chat_request['messageContent'];
@$sender_id = $chat_request['sender_id'];
@$subscribeChannel = $chat_request['subscribeChannel'];
@$attachmentType = $chat_request['attachmentType'];
@$event = $chat_request['event'];
我不知道这是否是 RESTful API 中的正确做法。我了解 POST 和 GET 之间的区别。我的服务器端脚本,我只需要从客户端获取数据,以便从数据库中创建、更新、读取和删除数据。 RESTful API 中的 PUT、DELETE 和 PATCH 有什么特别之处?
【问题讨论】:
-
网上有很多解释这些的文章。您是否尝试阅读其中任何一本?
标签: javascript php mysql angularjs