behrooz dalvandi behrooz dalvandi - 1 year ago 78
AngularJS Question

Why this angular $http.put paramers won't get passed?

I am facing a very weird case in my angularjs app. In a factory, the following code works properly:

$http.put(apiBase + 'delete?id='+orderId);

Which obviously connects to an api to perform a PUT operation (it is called "delete" here but it actually only updates a flag in the record).

But same code, when written this way, does not work:

$http.put(apiBase + 'delete', {
params: {
id: orderId

which is funny, because I am using the exact same syntax in some other factories to hit similar APIs and they work!

Answer Source

When using $http.put, you don't need to wrap your data in the config object. You can pass the data object directly, and then omit the third parameter:

$http.put(apiBase + 'delete', { id: orderId });

Your other factories probably work with the syntax stated in your question because you are making $http.get or $http.delete requests.

I have found that this slightly-different API for the various "shortcut" methods to be confusing enough that I almost think it's better to avoid them altogether. You can see the differences from the documentation where get and delete have two parameters:

get(url, [config]);
delete(url, [config]);

and most of the other shortcut methods have three:

post(url, data, [config]);
put(url, data, [config]);

Note that the [config] object is defined further up on that documentation page, which is where it defines that "params" property:

params – {Object.} – Map of strings or objects which will be serialized with the paramSerializer and appended as GET parameters.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download