简体   繁体   English

AngularJS $ http.post()触发get请求而不是post

[英]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. 我在角度和laravel中构建一个API服务,当我向API发出GET调用时,Everythings工作正常,但是当我触发POST调用时,服务仍然使用GET方法而不是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 : 我尝试将参数作为字符串传递(“a = b&c = d ...”)并添加标题:

$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. 在我的App模块实例化的run函数中但是,我一直得到405 (Method Not Allowed)错误。 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. $ http设置为POST但服务器从GET请求返回错误。

After checking the headers in a web inspector it shows the browser actually did two requests: 检查Web检查器中的标头后,它显示浏览器实际上执行了两个请求:

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. 第一个是$ http,第二个是重定向。 As you can see the trailing slash URL is redirected to a non trailing one. 如您所见,尾随斜杠URL被重定向到非尾随URL。 With this redirect a POST request gets also changed to GET as well. 通过此重定向,POST请求也会更改为GET。

The solution is to change your request url to not contain trailing slashes: 解决方案是将您的请求URL更改为不包含尾部斜杠:

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 GET工作正常...好POST返回405 - 方法不允许听起来它正在进行POST而你发布的服务器不支持对相关端点的POST请求

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 您能否提供更多信息,例如发出GET请求时的HTTP请求和响应标头,以及POST请求的相同信息

You can access the header information via the NET tab in Firefox's Firebug or in Chrome console 您可以通过Firefox的Firebug或Chrome控制台中的NET选项卡访问标题信息

  1. Be sure that your API method is ready to handle a POST request. 确保您的API方法已准备好处理POST请求。 Maybe Angular is actually firing a POST request, but your method is expecting a GET. 也许Angular实际上是在发出一个POST请求,但你的方法是期待一个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: 如果您确定Angular由于某种原因确实正在触发GET请求而不是POST,请尝试在$ http对象上显式设置HTTP方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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