简体   繁体   English

Angular默认将所有$ http请求转换为GET请求

[英]Angular Defaults all $http requests to GET requests

I am doing an angular project with Laravel 5. I have been developing this locally and works absolutely fine. 我正在与Laravel 5做一个有角度的项目。我一直在本地进行开发,并且工作得非常好。 All requests translate properly. 所有请求均正确翻译。

eg. 例如。 here is my code that sets the http method 这是设置http方法的代码

var params = {
     url:opts.url,
     method:opts.method
};

params.params = data;

$http(params).success(function(){});

If my opts.method is GET it says its GET if its POST it says its POST. 如果我的opts.method是GET,则表示其GET,如果其POST则表示其POST。

As soon as I host it on my digital ocean droplet my opts.method is a POST or a DELETE or a PUT it will turn automatically turn to a GET request. 一旦将其托管在数字海洋飞沫上,我的opts.method就是POST或DELETE或PUT,它将自动转向GET请求。

This is obviously a problem as I need to use POST and DELETE requests. 这显然是个问题,因为我需要使用POST和DELETE请求。

Does anyone have any ideas on why this would be failing? 有谁知道为什么会失败吗?

While hosted on the server I console logged the params object 当托管在服务器上时,我控制台记录了params对象

Object {url: "/api/update/", method: "POST", params: Object}

This still tried to send a GET request to the server. 这仍然尝试将GET请求发送到服务器。

I don't think it will be server side doing it as if i fake the requests its going through fine, and Laravel will return 405 method not allowed as soon as a GET request tries to go to a route that is made for POST requests. 我不认为这将是服务器端的工作,就好像我在假冒请求一样,并且一旦GET请求尝试转到为POST请求创建的路由时,Laravel将返回不允许的405方法。

Here are server details anyway, using Ubuntu running Apache2. 无论如何,这是使用运行Apache2的Ubuntu的服务器详细信息。 Any help would be much appreciated. 任何帮助将非常感激。

What if you used one of the shortcut methods? 如果使用一种快捷方式怎么办?

$http.get('/someUrl', config).then(successCallback, errorCallback);
$http.post('/someUrl', data, config).then(successCallback, errorCallback);

or in your case: 或者您的情况:

$http[opts.method.toLowerCase()](...)

see https://docs.angularjs.org/api/ng/service/ $http 参见https://docs.angularjs.org/api/ng/service/ $ http

disclaimer: I know that this doesn't answer the question but I don't have enough rep to comment yet. 免责声明:我知道这不能解决问题,但是我没有足够的代表对此发表评论。

It was trailing slashes. 那是斜线。 In Laravel routes it was /api/update in angular I used /api/update/ which for some reason made angular use a get request. 在Laravel路由中,角度是/ api / update,我使用了/ api / update /,出于某种原因,它使角度使用了get请求。

As soon as I changed angular to match the trailing slashes of the Laravel routes. 一旦更改角度以匹配Laravel路线的尾部斜线。 It worked fine. 工作正常。

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

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