I have the following resource definition:
myservices.factory('User', ['$resource',
function($resource){
return $resource('/user/:screenName', {}, {
whoami: {
url: '/user',
method: 'GET',
isArray: false,
params: {
op: 'whoami'
}
},
query: {
url: '/user',
method: 'GET',
isArray: true
},
get: {
method: 'GET',
isArray: false
},
update: {
method:'GET',
params: {
op: 'update'
},
isArray: false,
},
delete: {
method:'GET',
params: {
op: 'delete'
},
isArray: false,
}
});
}]);
and I was thinking it will pass screenName as the part of URL. Unfortunately, this does not happen.
The controller code is follows:
var user = new User();
user.firstname = $scope.selectedUser.firstName;
user.lastname = $scope.selectedUser.lastName;
user.screenName = $scope.selectedUser.screenName;
user.password1 = $scope.selectedUser.password1;
user.password2 = $scope.selectedUser.password2;
user.roles = $scope.selectedUser.roles;
user.maximalCalories = $scope.selectedUser.maximalCalories;
user.$update();
Actually it passes:
GET http://localhost:8080/user?op=update HTTP/1.1
Host: localhost:8080
...
Accept: application/json, text/plain, */*
...
Referer: http://localhost:8080/app/index.html
i.e. it passes neither parameters except explicit one.
UDPATE
If I do
User.$update(
{
firstname : $scope.selectedUser.firstName,
lastname : $scope.selectedUser.lastName,
screenName : $scope.selectedUser.screenName,
password1 : $scope.selectedUser.password1,
password2 : $scope.selectedUser.password2,
roles : $scope.selectedUser.roles,
maximalCalories : $scope.selectedUser.maximalCalories,
}
);
I get an exception TypeError: User.$update is not a function
UPDATE 2
Apparently, Angular adds function $update to the object and function update to the class, and if I have object at LHS, it will pass only by POST...
You are not passing screenName to resource. Should be:
user.$update({screenName: $scope.selectedUser.screenName});
The problem is you have wrong request type for you $resource's udpate object, It should have method PUT instead of GET
update: {
method: 'PUT', //<--change it 'PUT' from 'GET'
params: {
op: 'update'
},
isArray: false,
},
Also you had wrong type for delete method, it should have method: 'DELETE' instead of method: 'GET'
Related
I'm trying to use nuxt-auth module, my settings for this module is
auth: {
cookie: false,
plugins: ['~/plugins/api.js'],
redirect: {
logout: '/login',
login: '/',
home: false
},
strategies: {
local: {
scheme: 'refresh',
token: {
property: 'token',
maxAge: 3600
},
refreshToken: {
property: 'refresh_token',
data: 'refresh_token',
maxAge: 60 * 60 * 24 * 30
},
user: {
property: 'userDetail'
},
endpoints: {
login: { url: 'http://localhost:8085/api/login_check', method: 'post', propertyName: 'token' },
refresh: { url: 'http://localhost:8085/api/token/refresh', method: 'post', propertyName: 'refresh_token' },
logout: false,
user: { url: 'http://localhost:8085/api/user/fetchactive', method: 'get' }
},
tokenRequired: true
}
}
}
My "fetchactive" API returns a JSON containing a property "userDetail" which is a string containing the email address, (I also tried to make userDetail an object but with no luck).
e.g.
{"userDetail":{"email":"my#email.test"}}
Nuxt auth keeps telling me that "User Data response does not contain field userDetail".
I also tried to set "property" to false, but Nuxt auth in that cases looks for a field named "false"...
I just can't get it to work.
Anyone can help?
using this configuration in the nuxt.config.js file worked for me
auth: {
strategies: {
local: {
user: {
property: ''
},
//other configs
}
}
//other configs
}
It's happened due to the fact that auth.user endpoint search for 'data' object by default in the response but I does not exist there, as You have Your own response object key, not contained in 'data:{...}
try to add propertyName as in example in auth.user
endpoints: {
user: { url: 'http://localhost:8085/api/user/fetchactive', method: 'get', propertyName: '' }
}
}
I am developing multi-language website using Angularjs and a Web api as backend. I am trying to send RequestedPlatform and RequestedLanguage in the header whenever I make an API call.
Below is my Ajax request call.
$http.post(url,RegistrationData).then(function (response) {
var pageList = response.data.ID;
toastr.success('', 'Registered Succesfully');
$state.go('Registration.OTPVerification', { pageList });
}, function (error) {
toastr.error('', 'Error Occured');
});
updated code
var RegistrationData = {
FirstName: $scope.user.Fname,
LastName: $scope.user.Lname,
Password: $scope.user.password,
Gender: "Male",
DateOfBirth: "2017-04-04",
Nationality: $scope.user.selectedGlobe,
Mobile_CountryCod: "91",
MobileNumber: $scope.user.mobilenumber,
EmailId: $scope.user.email,
Home_Location: $scope.user.homeLocation,
Home_City: $scope.user.homeCity,
Home_Neighbourhood: $scope.user.homeNeighbourhood,
Home_HouseNumber: $scope.user.housenumber,
Home_MainStreet: $scope.user.homemainstreet,
Home_SubStreet: $scope.user.homesubstreet,
Work_Location: $scope.user.worklocation,
Work_City: $scope.user.workcity,
Work_Neighbourhood: $scope.user.workNeighbourhood,
Work_HouseNumber: $scope.user.workhousenumber,
Work_MainStreet: $scope.user.workmainstreet,
Work_SubStreet: $scope.user.worksubstreet
};
var req = {
method: 'POST',
url: url,
data: { RegistrationData: RegistrationData },
headers: {
RequestedPlatform: "Web",
RequestedLanguage: "English"
}
}
$http(req).then(function (response) {
var pageList = response.data.ID;
toastr.success('', 'Registered Succesfully');
$state.go('Registration.OTPVerification', { pageList });
}, function () {
toastr.error('', 'Error Occured');
});
May I get some help to set headers in Ajax. Any help would be appreciated.
you can send headers with headers property of $http
var req = {
method: 'POST',
url: 'http://example.com',
headers: {
'Content-Type': undefined
},
data: { test: 'test' }
}
$http(req).then(function(){...}, function(){...});
and if you want headers for all the requests that can be fully configured by accessing the $httpProvider.defaults.headers configuration object,
Reference
There are few ways and I have posted one which I have been using it for a while. I hope you are looking for the below
$http.post('test', data, {
withCredentials : false,
transformRequest : angular.identity,
headers : {
'Content-Type' : undefined
}
})
I have the next resource:
angular.module('app').factory 'User', [
'$resource'
($resource) ->
$resource '/users/:id/:action', {
id: '#id'
action: '#action'
},
query:
method: 'GET'
isArray: false
'update':
method: 'PUT'
transformRequest: (data) ->
JSON.stringify user: data
'resetPassword':
params: action: 'reset_password'
method: 'PUT'
]
And I want to unify it with the next one:
angular.module('app').factory "NewUser", ["RailsResource", "railsSerializer", (RailsResource, railsSerializer) ->
class NewUser extends RailsResource
#configure
url: '/users'
rootWrapping: false
serializer: railsSerializer ->
#nestedAttribute "addresses", "address"
NewUser
]
The goal is to have both of the above resources merged into a single one.
What's the best approach for doing this?
I have a service like bellow, how is it possible to call "admins remove" from controller? I know, how to call it without user or admin "scope" (promToRemove.$remove()) but in this situation I am despairing. I also know, how to successfully call get.
angular.module('users').factory('Users', ['$resource', function($resource) {
return {
user: $resource('users/:userId', { userId: '#_id' },
{ get: { method: 'GET' }},
{ update: { method: 'PUT' }}
),
admin: $resource('admin/users/:userId', { userId: '#_id' },
{ get: { method: 'GET' }},
{ update: { method: 'PUT' }},
{ remove: { method: 'DELETE' }}
)
};
}]);
If you want to issue a DELETE command to that API endpoint, try calling the $delete method on that object.
userIWantToDelete.$delete(function() {});
But It will need to have the _id property.
Being relatively new to Angular, I would like some assistance in setting up a new factory that uses ngResource, rather than $http, and that I can pass parameters to.
Based on this example, I have setup a factory as follows:
app.factory('abstractFactory2', function ($resource) {
//var odataUrl = "/odata/ContentType";
return $resource("", {},
{
'getAll': { method: "GET", url: odataUrl, params: {options: "#options" } },
'save': { method: "POST", url: odataUrl, params: { options: "#options" } },
'update': { method: 'PUT', params: { key: "#key" }, url: odataUrl + "(:key)" },
'query': { method: 'GET', params: { key: "#key" }, url: odataUrl + "(:key)" },
'remove': { method: 'DELETE', params: { key: "#key" }, url: odataUrl + "(:key)" }
});
});
I have been able to pass in the value of odataUrl from the controller, but experiencing some odd behavior.
In my controller, the call in my data setup is:
// TODO: pass in odata URL and odata options to factory
var dataSource = new kendo.data.DataSource({
type: "odata",
transport: {
read:
function (options) {
var odataParams = kendo.data.transports["odata"].parameterMap(options.data, "read"); // Object {$inlinecount: "allpages", $format: "json", $top: 10}
(new abstractFactory2.query({ options: odataParams })).$getAll()
.then(function (data) {
return options.success(data);
});
}, . . .
I would like to be able to pass in the odataUrl (as well as other parameters), and the OData options from the function in my controller, and make subsequent appropriate update, create and destroy calls to the factory.
In Fiddler I see that there are two requests to the OData URL, one with partial parameters:
I tried passing in odataUrl = '/odata/contentType' and reading it in the factory withurl: "#odataUrl"` but haven't been able to get that working.
GET /odata/ContentType
GET /odata/ContentType?options=%7B%7D
I would also like to be able to hook in to the .success() and .error() events of the call.
might not the best way but can't you just say url: odataUrl + params