Angularjs获得OPTIONS和POST请求

时间:2016-09-04 01:03:57

标签: php angularjs cors

我正在向一个不同的域发送POST请求,例如angular:

keyValue.size = strlen(key);
dataValue.size = strlen(value);

在我的请求日志中,它只发送了一个OPTION请求。

所以我将其添加到我的$http.post('http://domain.com/auth', { email: $scope.email, password: $scope.password })

domain.com/auth

但是现在,我首先获得OPTION请求,然后是POST请求。因为我的OPTION请求是第一个,我认为它仍然在弄乱我的结果。

有谁知道如何只获取POST请求而不是OPTIONS请求?

1 个答案:

答案 0 :(得分:2)

您可以通过触发simple request

来阻止POST上的预检

要实现此目的,请使用$httpParamSerializerJQLike对数据进行编码(确保注入序列化程序)并将内容类型设置为application/x-www-form-urlencoded

$http({
  url: 'YOUR_ENDPOINT',
  method: 'POST',
  data: $httpParamSerializerJQLike(YOUR_DATA),
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  }
})
相关问题