Angularjs $http.post不发送头

Angularjs $http.post不发送头,angularjs,Angularjs,我创建了一个基本的$http.post请求来接收一些数据。为了接收数据,我需要在标题中提供一个键 即使我已显式发送密钥,服务器仍响应密钥丢失 100%不是服务器,因为我使用了一个在线curl工具,它提供了所需的响应 $http.post(url, { data:{username: username,password: password}, headers:{'Content-Type': 'application/x-www-form-urlencoded','

我创建了一个基本的$http.post请求来接收一些数据。为了接收数据,我需要在标题中提供一个键

即使我已显式发送密钥,服务器仍响应密钥丢失

100%不是服务器,因为我使用了一个在线curl工具,它提供了所需的响应

$http.post(url, {        
    data:{username: username,password: password},
    headers:{'Content-Type': 'application/x-www-form-urlencoded','key': key},
  })
  .then(function(response){
    console.log(response)
  })

如果有人能发现我做错了什么,我将不胜感激。谢谢。

您在headers键后面有一个逗号,也许这是在浪费时间

$http.post(url, {        
  data:{username: username,password: password},
  headers:{'Content-Type': 'application/x-www-form-urlencoded','key': key}, // Remove this comma
})
.then(function(response){
  console.log(response)
});

我以前也遇到过类似的问题,通过在app config阶段向httpProvider添加transformRequest解决了这个问题。感谢在此处提供代码的kzar:

根据该报告,应:

捷径法

长版本


经过测试,工作正常。

我会使用浏览器开发人员工具嗅探XHR请求,以确保标头未正确发送。例如,在Chrome中,您可以使用开发人员工具检查XHR请求,并查看从请求发送的所有内容。但现在它不接受我随它发送的数据。有什么想法吗?什么类型的后端服务器正在处理请求?也许您需要研究如何正确处理数据,或者如果需要,对其进行编码/解码
  $http.post('http://localhost:9191/api/signin', {
    username: 'some username',
    password: 'some password'
  }, {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'key': '123'
    }
  })
  .then(function(response) {
    console.log(response)
  });
$http({
  method: 'POST',
  url: 'http://localhost:9191/api/signin',
  data: {
    username: 'some username',
    password: 'some password'
  },
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'key': '123'
  }
}).then(function(response) {
  console.log(response)
});