AngularJS-$resource获取和发布的不同URL
$resource提供了非常方便的方式来处理web服务。 如果GET和POST必须在不同的URL上执行,该怎么办 例如,GET URL是AngularJS-$resource获取和发布的不同URL,angularjs,Angularjs,$resource提供了非常方便的方式来处理web服务。 如果GET和POST必须在不同的URL上执行,该怎么办 例如,GET URL是http://localhost/pleaseGethere/:id 而POST URL是http://localhost/pleasePosthere没有任何参数您应该能够将URL作为参数公开。我能够做到这一点: $provide.factory('twitterResource', [ '$resource', function($resou
http://localhost/pleaseGethere/:id
而POST URL是
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://
,它将不起作用。我没有收到错误消息。它什么也没做。如果将带有参数名的哈希添加到$resource调用中:
$resource('localhost/pleaseGethere/:id', {id: '@id'});
然后:id将在调用函数时映射到id参数(这将调用GET localhost/plesegether/123):
对于POST,您只需不分配id参数:
将调用正确的URL,在本例中是POST localhost/plesegether(后面的斜杠由ngResource去掉)
有关详细信息,请参见->示例->信用卡资源。使用[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
}
}
Angular$资源的使用情况:请按照以下方式操作:
(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);
}
})();
我希望它能帮上忙!享受吧 除了Iris Wong的答案之外,我还想举一个例子,说明使用多个方法和操作的多个参数:
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'})
问题是url参数被编码,这就是为什么“http://”或任何带有“/”的内容都会失败的原因。有什么想法吗?@Zymotik这个答案对于询问者所寻找的东西来说有点太复杂了-Iris的答案是正确的。在“基本”url中使用的
:id
也可以在postMethod url中找到+1.全面而有价值+1.
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'
}
});
}
// 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'})