简体   繁体   中英

AngularJS $http.post() firing get request instead of post

i building an API service in angular and laravel, when i firing a GET call to the API everythings work fine, but when i fire POST call the service still use GET method instead of POST. that is my service:

function LeadsAPI($http,$q,BASE_URL)
{
   this.updateLead = function (lead_data) {
        var url = BASE_URL+"/leads/update/";
        var deferred = $q.defer();
        $http.post(url , lead_data).then(function(response){
            deferred.resolve(response.data);
        });
        return deferred.promise;
    }
}

i call to this function from a Controller:

LeadsController.$inject = ['$scope', 'LeadsAPI'];
function LeadsController($scope , LeadsAPI)
{
  LeadsAPI.updateLead({'lead_id' : res._id, 'the_lead': {'fist_name' : 'asd asd'}}).then(function (res) {
            console.log(res);
        });
}

i tried pass the parameters as a string ("a=b&c=d...") and added header :

$http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';

in the run function at my App module instantiation but yet, i keep getting 405 (Method Not Allowed) error. any ideas why and how to solve it? thank you very much all! :)

Seems the question is old and unanswered but google led me here. I hope someone will find this answer useful.

I had the same problem. $http was set to POST but server was returning error from GET request.

After checking the headers in a web inspector it shows the browser actually did two requests:

update/ 301     text/html   angular.js:11442        
update  405     xhr         https://test.site/post/update

The first one is the one from $http and the second one is after a redirect. As you can see the trailing slash URL is redirected to a non trailing one. With this redirect a POST request gets also changed to GET as well.

The solution is to change your request url to not contain trailing slashes:

url: BASE_URL+"/leads/update",

The GET works fine ... good The POST returns 405 - Method not allowed It sounds like it is doing a POST and the server you are posting to does not support POST requests to the endpoint in question

Can you please provide more information, such as the HTTP request and response headers when you make a GET request and the same for the POST request

You can access the header information via the NET tab in Firefox's Firebug or in Chrome console

  1. Be sure that your API method is ready to handle a POST request. Maybe Angular is actually firing a POST request, but your method is expecting a GET.

  2. If you are sure Angular is really firing a GET request instead of a POST for some reason, try to explicitly set the HTTP method on the $http object:

     $http({ method: 'POST', url: BASE_URL+"/leads/update/", data: lead_data }).then(function (response) { deferred.resolve(response.data); }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM