AngularJS $http 和 $resource [英] AngularJS $http and $resource
问题描述
我有一些网络服务要调用.$resource
或 $http
,我应该使用哪个?
I have some web services that I want to call. $resource
or $http
, which one should I use?
$resource
:https://docs.angularjs.org/api/ngResource/service/$resource
$http
:https://docs.angularjs.org/api/ng/service/$http
读完上面两个 API 页面后,我迷路了.
After I read the two above API pages I am lost.
您能否用简单的英语向我解释一下有什么区别,我应该在什么情况下使用它们?如何构建这些调用并将结果正确读入 js 对象?
Could you please explain to me in plain English what is the difference and in what situation should I use them? How do I structure these calls and read the results into js objects correctly?
推荐答案
$http
用于通用 AJAX.在大多数情况下,这就是您将要使用的.使用 $http
,您将手动进行 GET
、POST
、DELETE
类型调用并处理对象他们自己回来.
$http
is for general purpose AJAX. In most cases this is what you'll be using. With $http
you're going to be making GET
, POST
, DELETE
type calls manually and processing the objects they return on your own.
$resource
包装 $http
以在 RESTful Web API 场景中使用.
$resource
wraps $http
for use in RESTful web API scenarios.
笼统地说:RESTful Web 服务将是一种具有一个数据类型端点的服务,该数据类型基于 GET
、POST等 HTTP 方法对该数据类型执行不同的操作code>、
PUT
、DELETE
等.所以使用$resource
,你可以调用GET
来获取资源作为 JavaScript 对象,然后更改它并使用 POST
将其发送回,甚至使用 DELETE
将其删除.
Speaking VERY generally: A RESTful web service will be a service with one endpoint for a data type that does different things with that data type based on HTTP methods like GET
, POST
, PUT
, DELETE
, etc. So with a $resource
, you can call a GET
to get the resource as a JavaScript object, then alter it and send it back with a POST
, or even delete it with DELETE
.
...如果这是有道理的.
... if that makes sense.
这篇关于AngularJS $http 和 $resource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!