$ http post does not work after migration from angularJS 1.3-beta to 1.4

I am moving my project from angularjs 1.3 beta to 1.4, "$ http get" works fine, but the '$ http post' creates a problem. The following is a user authentication code that works fine in version 1.3 but does not work in version 1.4

factory.authenticate = function (email, password) { var payload = { email: email, password: password }; var rid = Security.reqKey(); payload['rid'] = rid; return $http({ method: 'POST', url: Base_url +'service/auth', param: { rid: rid }, data: $.param(payload), headers: {'Content-Type': 'application/x-www-form-urlencoded'} }); }; 

After switching to 1.4, it causes an error:

POST http: // localhost / 405 (not allowed) XHR could not load: POST

(which worked great before)

There is a case when this code gives the desired result. If I put the debugger in the Chrome browser and run all the code by pressing f10.

+5
source share
1 answer

According to the docs ,

params - {Object.} - A map of strings or objects that will be serialized using paramSerializer and added as GET Parameters.

param should be params , and therefore your data is not serialized, which explains why the server rejects your POST (since the post requires more privileges than GET, supposedly).

But even this may not be true, since I think that params is for GET only (but maybe it works for both).

You also don't need jquery $.param() $ http has a built-in serializer. data can just set the payload value.

0
source

Source: https://habr.com/ru/post/1233526/


All Articles