$资源是真棒提供非常方便的方式来处理Web服务。 如果GET和POST必须在不同的URL上执行,该怎么办?
例如,获取URL是http://localhost/pleaseGethere/:id
和POST网址为http://localhost/pleasePosthere
不带任何参数
$资源是真棒提供非常方便的方式来处理Web服务。 如果GET和POST必须在不同的URL上执行,该怎么办?
例如,获取URL是http://localhost/pleaseGethere/:id
和POST网址为http://localhost/pleasePosthere
不带任何参数
您应该能够揭露URL作为参数。我能做到这一点:
$provide.factory('twitterResource', [
'$resource',
function($resource) {
return $resource(
'https://:url/:action',
{
url: 'search.twitter.com',
action: 'search.json',
q: '#ThingsYouSayToYourBestFriend',
callback: 'JSON_CALLBACK'
},
{
get: {
method: 'JSONP'
}
}
);
}
]);
则可以覆盖你的GET
呼叫URL。
我在我真正简短的测试中发现的一个警告是,如果我在URL字符串中包含http://
,它不起作用。我没有收到错误消息。它什么都没做。
如果添加与PARAM的散到$资源调用:
$resource('localhost/pleaseGethere/:id', {id: '@id'});
然后:ID将被映射到ID调用函数时参数(这将调用GET本地主机/ pleaseGethere/123):
Resource.get({id: 123});
对于POST,你根本不分配ID PARAM:
Resource.post({}, {name: "Joe"});
正确的URL将被调用,这是在这种情况下,POST 本地主机/ pleaseGethere(结尾的斜线被ngResource剥离)。
请参阅http://docs.angularjs.org/api/ngResource.$resource - >示例 - >信用卡资源以获取更多详细信息。
使用[actions]的'url'属性来覆盖默认的url。
$resource(url, [paramDefaults], [actions], options);
例如:
$resource('http://localhost/pleaseGethere/:id',{},{
getMethod:{
method:'GET',
isArray:true
}
postMethod:{
url:'http://localhost/pleasePosthere',
method:'POST',
isArray:false
}
}
使用角$资源:http://docs.angularjs.org/api/ngResource/service/$resource
“base”网址中使用的':id'也可以在postMethod url中使用。 +1! –
按照这种方式:
(function() {
'use strict';
angular
.module("app")
.factory("SomeFactory", SomeFactory);
function SomeFactory($resource) {
var provider = "http://stackoverflow.com/:action/:id";
var params = {"id":"@id"};
var actions = {
"create": {"method": "POST", "params": {"action": "CreateAwesomePost"}},
"read": {"method": "POST", "params": {"action": "ReadSomethingInteresting"}},
"update": {"method": "POST", "params": {"action": "UpdateSomePost"}},
"delete": {"method": "GET", "params": {"action": "DeleteJustForFun"}}
};
return $resource(provider, params, actions);
}
})();
我希望它能帮助!请享用!
除了虹膜黄的回答,我想给具有多种方法和措施多则params的一个例子:
angular
.module('thingApp')
.factory('ThingResource', ['$resource', '$state', returnThing]);
和资源:
function returnThing($resource, $state) {
var mainUrl = '/api/stuffs/:stuffId/thing'
var params = {stuffId: '@_id', thingMongoId: '@_id', thingNumber: '@_id'}
return $resource(mainUrl, params, {
'save': {
url: '/api/stuffs/:stuffId/thing/:thingMongoId',
method: 'POST',
interceptor: {
responseError: function(e) {
console.warn('Problem making request to backend: ', e)
$state.go('oops')
}
}
},
'get': {
url: '/api/stuffs/:stuffId/thing/:thingMongoId',
method: 'GET',
interceptor: {
responseError: function(e) {
console.warn('Problem making request to backend: ', e)
$state.go('oops')
}
}
},
'assignThing':{
method: 'POST',
url: '/api/stuffs/:stuffId/thing/assign/:thingNumber'
}
});
}
其中给出3种独立的方法:
// POST to http://currnt_base_url/api/stuffs/:stuffId/thing/:thingMongoId
ThingResource.save({
stuffId:'56c3d1c47fe68be29e0f7652',
thingMongoId: '56c3d1c47fe6agwbe29e0f11111'})
// GET to current http://currnt_base_url/api/stuffs/:stuffId/thing/:thingMongoId
ThingResource.get({
stuffId:'56c3d1c47fe68be29e0f7652',
thingMongoId: '56c3d1c47fe6agwbe29e0f11111'})
// POST to http://currnt_base_url/api/stuffs/:stuffId/thing/assign/:thingNumber
ThingResource.assignThing({
stuffId:'56c3d1c47fe68be29e0f7652',
thingNumber: '999998'})
全面和有价值。 +1 –
问题是url参数被编码,这就是为什么'http://'或任何与'/' 将失败。有任何想法吗? – Zymotik
@ Zymotik http://stackoverflow.com/questions/22944932/angularjs-resource-how-to-disable-url-entity-encoding – cameronroe
这个答案有点过于复杂的提问者寻找 - 虹膜的答案是点。 – btk