$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);
}
});
当我使用此代码时,我的 chrome 会发送:
Request URL:http://cards.mporeda.pl/branch/api/getcard
Request Method:GET
Status Code:405 Method Not Allowed
但是当我在 laravel serve localhost:8000 上使用相同的代码时,我得到:
Request URL:http://localhost:8000/branch/api/getcard/
Request Method:POST
Status Code:200 OK
我没有更多的 $http 配置,只有请求中的这个 header 选项。在此请求之前,我在控制台上没有错误,所以我怀疑我的代码没问题。我的服务器有什么问题吗?
最佳答案
您的代码表示要向其发出请求的 URL 是:
main+'/api/getcard/'
您的请求表明您正在使用的 URL 是:
Request URL:http://cards.mporeda.pl/branch/api/getcard
这很可能是由于:
- 您向您尝试向其发出 POST 请求的 URL 发出 POST 请求
- 服务器以 301 或 302 状态和
Location
header 响应,该 header 重定向到末尾没有/
的相同 URL - 跟随重定向并发出 GET 请求的浏览器
如果您查看请求列表,您应该会看到 POST 请求。
要解决此问题,您需要查看发出重定向的服务器端代码。
关于javascript - Angular $http 发送 get 而不是 post,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42873563/