Angular $ http发送get而不是post

Mic*_*eda 2 javascript methods http laravel angularjs

$http.post(main+'/api/getcard/', $.param({number: $scope.searchcard}), {headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'} })
        .then(function (response) {
            if(response.data != 0)
            {
                $location.path('/redeem/'+response.data.id);
                console.log(response.data);
            }
        });
Run Code Online (Sandbox Code Playgroud)

当我使用此代码时,我的chrome发送:

Request URL:http://cards.mporeda.pl/branch/api/getcard
Request Method:GET
Status Code:405 Method Not Allowed
Run Code Online (Sandbox Code Playgroud)

但是当我在laravel服务localhost:8000上使用相同的代码时,我得到:

Request URL:http://localhost:8000/branch/api/getcard/
Request Method:POST
Status Code:200 OK
Run Code Online (Sandbox Code Playgroud)

我在请求中没有任何更多的$ http配置.在此请求之前我在控制台上没有错误,所以我问我的代码是否正常.我的服务器有什么问题吗?

Que*_*tin 8

您的代码发出请求的URL是:

main+'/api/getcard/'
Run Code Online (Sandbox Code Playgroud)

您的请求所使用的网址是:

Request URL:http://cards.mporeda.pl/branch/api/getcard
Run Code Online (Sandbox Code Playgroud)

这很可能是由以下原因引起的:

  1. 您对尝试发出POST请求的URL发出POST请求
  2. 服务器以301或302状态响应,并且Location标头重定向到同一个URL,而不是/最后一个
  3. 重定向后的浏览器并发出GET请求

如果您查看请求列表,则应该看到POST请求.

要解决此问题,您需要查看发出重定向的服务器端代码.