简体   繁体   English

Angularjs $ http服务POST,PUT和GET

[英]Angularjs $http Service POST, PUT and GET

I am developing application in AngularJS. 我正在AngularJS中开发应用程序。 But I still not so clear the difference of POST, PUT and GET. 但是我仍然不太清楚POST,PUT和GET的区别。 Usually I use $http GET when I get data from server when server side does not require any front end data to return data to client side as below. 通常,当服务器端不需要任何前端数据将数据返回到客户端时,当我从服务器获取数据时,我会使用$ http GET,如下所示。

$http.get(configSettings.baseUrl +"retrive_user.php").success(function (data) {

}).error(function() {
    console.log("error");
});

When I am using POST is when I server side require front end data in order to return data to client side as following. 当我使用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);
    })
});

Even, I want to delete data or edit data in my MySQL database I am using POST method in angularjs as above and then in my PHP server side, I do like following to get data. 甚至,我想删除我的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'];

I don't know whether this is a correct practice in RESTful API. 我不知道这在RESTful API中是否正确。 I understand the difference between POST and GET. 我了解POST和GET之间的区别。 I my server side scripting, I just need to get data from client side in order to Create, Update, Read and Delete data from database. 在我的服务器端脚本编写中,我只需要从客户端获取数据即可从数据库中创建,更新,读取和删除数据。 What so special about PUT, DELETE and PATCH in RESTful API? RESTful API中的PUT,DELETE和PATCH有何特别之处?

HTTP verbs are probably one of the most cryptic things about the HTTP protocol. HTTP动词可能是HTTP协议中最隐秘的事物之一。

PUT replace the ENTIRE RESOURCE with the new representation provided or you can say that if user want to add new record, he should use PUT . PUT用提供的新表示替换整个资源 ,或者您可以说,如果用户要添加新记录,则应该使用PUT

On the other hand PATCH => As the name said its a kind of patch to update a piece of record. 另一方面PATCH =>顾名思义,它是一种用于更新记录的补丁。 If user can only wants to update a partial record , say just an email address, he should use PATCH. 如果用户只想更新部分记录 (仅说一个电子邮件地址),则应使用PATCH。

As PUT method can update all the record so it need more bandwidth or handle full resources instead on partial. 由于PUT方法可以更新所有记录,因此它需要更多的带宽或处理全部资源,而不是部分地处理。 So PATCH was introduced to reduce the bandwidth . 因此引入了PATCH以减少带宽

For example :- lets say I am sending new record to server ie, 例如:-可以说我正在向服务器发送新记录,即

{ "first": "Anand Deep", "last": "Singh" } 

So I will use Put because I am adding new record. 所以我将使用Put,因为我要添加新记录。 But here has one problem with Put request that when I will use PUT, I have to send all two parameters that is first and last again. 但是这里的Put请求有一个问题,当我将要使用PUT时,我必须发送所有两个参数,这是第一个参数,也是最后一个参数。 so it is mandatory to send all value again 因此必须再次发送所有值

But Patch only send the data which user want to update and it won't effecting or changing other data.So no need to send all value again. 但是Patch仅发送用户要更新的数据,而不会影响或更改其他数据,因此无需再次发送所有值。

So PUT for creating new record and PATCH is for updating existing record. 因此,用于创建新记录的PUT和用于更新现有记录的PATCH

Same for DELETE , its tell to server that this request should delete the record which pass it to server. DELETE相同,它告诉服务器此请求应删除将其传递给服务器的记录。

For more details click on below image or this URL :- 有关更多详细信息,请单击下面的图像或此URL

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM