Frage

I'm trying to send the post request to server with post data it's sent the request to the server, but not in right format

request url like /rest/api/modifyuser/?currentPassword=admin&newPassword=admin

it's like GET request - (may be this is problem)

I'm new to angularjs . please share idea to solve this problem

Here is my code

In controller
       var currentPass = "admin";
        var newPass = "admin";
        var confirmPass = "admin";

        var authToken = "abcdef";

        User.changePassword(currentPass, newPass, confirmPass, authToken, function(response) {
         angular.forEach(response, function (item) {
             alert("resp"+ item);
         });
        });

In services

UIAppResource.factory('User', function($resource) {
    return {
        changePassword: function(currentPass, newPass, confirmPass, authtoken, callback) {
           var Resq = $resource(baseURL + "modifyuser", {}, {
                'query': {
                    method: 'POST',
                    params: {
                        'currentPassword': currentPass,
                        'newPassword': newPass,
                        'confirmPassword': confirmPass
                    },
                    headers: {
                       'Accept':'application/json',
                       'Content-Type':'application/json',
                       'X-Internal-Auth-Token': authtoken
                    },
                    isArray: false
                }
            });
            Resq.query(callback);
        }
    };
});

Thanks in advance

War es hilfreich?

Lösung

I dont want to say you are doing it all wrong.. but you are def. abusing things. The default way to POST something with ng-resource is to use save. Second, the default way to send data is to instantiate a $resource factory with the data you want. See _resource below. We pass the data we want, and it will automagically convert it and if its a POST send it in the body, or in the case of a GET it will turn into query parameters.

UIAppResource.factory('User', function($resource) {
   return {
       changePassword: function(currentPass, 
                                newPass, 
                                confirmPass, 
                                authtoken, 
                                callback
       ) {
          var Resq = $resource(baseURL + "modifyuser", {}, {
            'save': {
                method: 'POST',
                headers: {
                   'Accept':'application/json',
                   'Content-Type':'application/json',
                   'X-Internal-Auth-Token': authtoken
                }             
            }
        });
        var _resource = new Resq({
                    'currentPassword': currentPass,
                    'newPassword': newPass,
                    'confirmPassword': confirmPass
        });
        _resource.$save(callback);
      }
  };
});
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top