I am trying to implement CRUD using $resource, i am not finding any issues in POST, PUT and GET. But on delete method the params are getting passed as query string.
My service:
service.Assigndepart = $resource(CONFIG.wsurl + '/employee/assign/depart',null, {
update: {
method: 'PUT'
},
remove:{
method: 'DELETE'
}
});
And calling my controller as
Assigndepart.remove(params, function(success), function(error));
my url is passing with query string as ?employee=1234&depart=456.
Can some help on this
See basically if you get into the Html apis in form tag for method type what you will see is only support for get and post method, which indicates that with post and get we can do everything we want all of the others are just good conventions and some code optimizations(like put method).The delete method is just similar to the get method which always includes the parameters as its param there is nothing you can do about it.if you want to remove that you have to use the structure of post or put requests only or you can still use the delete method with some encryption and decryption at both ends i.e. application server and front end.
According to the Angular $resource documentation the action methods without a body need to be invoked with the following parameters:
Resource.action([parameters], postData, [success], [error])
So you are passing your postData as params for your DELETE action. The correct call on your controller will be:
Assigndepart.remove({}, params, function(success), function(error));
Related
I have a Angularjs resource and in one of the methods, I want to send a query parameter to the backend if that query parameter is non-empty. Is it possible to achieve that without created a different method?
Example resource method:
get: {
method: 'GET',
url: /api/:token?source=:source,
paramSerializer: someSerializer
},
The code to call the method is,
myResource.get({token: '1234'}, {source: <some source>});
The token is required, but the source is optional, so sometimes it is not passed in to the call above. So if a value is provided for the source, I want to be able to send the source. If the source value is not provided, I want to not send the source at all. For instance, the call will become /api/1234 if source is empty, and the call will become /api/1234?source=some_source if the source is set as some_source.
With the code above, if the source is empty, I see that the request is '/api/1234?source=' when the source is empty.
Simply define the url template without the extra parameters:
get: {
method: 'GET',
url: "/api/:token",
},
Then add the extra parameters when invoking the action:
myResource.get({token: '1234', source: <some source>});
The extra parameters will be automatically added to the url as query parameters.
From the Docs:
Each key value in the parameter object is first bound to url template if present and then any excess keys are appended to the url search query after the ?.
For more information, see
AngularJS $resource API Reference - Arguments
If you pass query params via get.params{} then AngularJS will automatically handled stripping out undefined properties.
I have a resource in my API called /foos. I can create a new Foo by making a POST request to this URL.
In the client I use Angular's ngResource to deal with API calls. Insertion is easy:
var newFoo = new Foo({param1: 'value1'});
newFoo.$save();
So far, so good. The API returns the following:
{id: 1, param1: "value1"}
Now, here's the good part. For the sake of simplicity my API allows to specify which related entities should be returned in the response by means of a query string param, with. So:
GET /foos/1?with=relatedEntity
Would return
{id: 1, param1: "value1", relatedEntity: { ... }}
This is also accepted in POST requests. I'd like to create a Foo and have it returned back with some related entities so that I can use them straight away in my app without the need to perform another request:
POST /foos?with=relatedEntity
However, I've been unable to achieve this using standard the ngResource $save() function.
Can this be done at all?
My bad.
Our client extends the base $resource factory and overrides the $save() function, but this implementation was not propagating all the required parameters down the call chain, so no URL params were being sent.
In the following code, the controller is not invoking the third $http.get() call and instead goes directly goes to the end.
I want to execute all $http.get() requests. In my code, the third $http.get() depends on the second $http.get() result. Additionally, the second $http.get() depends on the first $http.get() result.
Code
Does anyone have an idea why the third $http.get() is not being invoked?
with the way you structured your code, this code below will run before the third $http.get
orderDetails['orderData'] = data;
orderDetails['kitNames'] = kitNames;
orderDetails['proteins'] = proteins;
orderDetails['dietaries'] = dietaries;
orderDetails['ingredients'] = ingredients;
you would have to put it in the third $http.get and every other code that depends on it, and that should solve your challenge
Also, your code can be refactored, so it is more readable and understandable for you to read
Suggestion:-
Actually this is a big issue with angular http call, Because http call does not support async :false
See my answer in this discussion : Angularjs $http VS jquery $.ajax
Your code look like a lot of confusions for call new http method inside of inside methods.
And your code looks like not standard format also gives lot of confusion.
So if you change any code in this file after some day's, then you need to put a microscope glass in your eye, and will see one by one lines. it's will take more times. So avoid http call only for this type of situation.
Solution:-
Avoid http call means, pleas do with Ajax call with async:false option
The code look like
$.ajax({
type: "GET",
dataType: "Json",
url: 'Your URL',
**async: false,**
success: function (returndata, status, jqxhr) {
$(this).html(returndata).hide().fadeIn();
}).fail(function() {
alert("error");
})
});
Explanation About Async:false
Setting async to false means that the statement you are calling has to complete before the next statement in your function can be called. If you set async: true then that statement will begin it's execution and the next statement will be called regardless of whether the async statement has completed yet.
So I'm trying to AJAX a single solr doc from my results list to a "doc view" view. I'm trying to use AngularJS to AJAX to my view render method and display the doc that way, but I can't seem to get the angular to work and I'm not sure I'm doing things correctly on the Play side either. Would you at least be willing to tell me if what I'm trying to do will work? The Angular error comes from the docText.text(); call. Here is my code:
Angular controller code:
var docText = $resource("http://localhost:9000/views/full-doc-text.html", {
text: {method: 'PUT'}
});
$scope.handleViewText = function(value) {
docText.text({doc: value});
}
Java code:
public static Result viewText() {
JsonNode json = request().body().asJson();
//do stuff here
return ok(viewtext.render(json));
}
route:
GET /views/full-doc-text.html controllers.Application.viewText()
I see three problems with the code above;
1.The definition of docText resource is not correct. if your read the angularjs manual here you'll see that $resource has 4 parameters. First one is resource url, second is parameter defaults, third one is custom actions and forth one is resource options where last three of them are optional. In your code you pass custom actions as the second parameter, which should be the third. And since you don't have any parameters in your resource url second parameter must be null. So first correction is:
var docText = $resource("http://localhost:9000/views/full-doc-text.html", null, {
text: {method: 'PUT'}
});
2.You define your text action's HTTP method as PUT however in your routes file you are handling GET requests for your desired action. You should change your route definition as:
PUT /views/full-doc-text.html controllers.Application.viewText()
3.PUT method is usually used for update operations when implementing a RESTFULL service. In your case you don't seem to be updating anything. So I suggest to use POST method just for convention.
I've really be trying to wrap my head around this as much as possible, but having a very difficult time doing so. Maybe I'm missing the obvious.
Given a typical REST API (with an idempotent update method and a create method):
http://www.domain.com/api/clients GET // returns all clients
http://www.domain.com/api/clients POST // create a new client
http://www.domain.com/api/clients/:id GET // returns single client
http://www.domain.com/api/clients/:id PUT // updates a single client - idempotent
http://www.domain.com/api/clients/:id DELETE // delete single client
If I create a standard resource with the following URL:
Client = $resource("http://www.domain.com/api/clients/:id")
Then I automatically get (where Client is the $resource and client is the returned entity):
Client.get()
Client.query()
Client.save()
client.$save()
client.$remove/delete()
The problem I have is by default there is no PUT method to save (typically used to identify idempotent updates).
Am I misunderstanding something or is this a deficiency in Angular's API? I would have expected the $save() to use a PUT and not a POST. The way it is currently structured, I have to create my own $update() method definition and then rely on the developer not to accidentally use the $save() method.
Am I structuring my API incorrectly? Should the REST API be structured differently?
You can simply specify the method in your resource like :
app.factory('someFactory', ['$resource', function($resource) {
return $resource('/api/:id', {
id: '#id'
}, {
update: {
method: 'PUT'
},
get: {
method: 'GET'
}
});
}]);
but I totally agree with $save being an odd verb for create and not update. This guy does too and it looks like he made a way to dual purpose the save by simply extending the object and checking for an id.