`$ http:badreq Bad Request Configuration` - 来自angular post方法,这里有什么问题?

时间:2016-09-11 02:29:36

标签: angularjs angular-http

我正在开发示例应用以使用angularjs了解node.js。当我将数据发布到后端以创建新的family我收到错误时:

Error: $http:badreq
Bad Request Configuration
Http request configuration url must be a string.  Received: 
{
    "method":"POST",
    "url":"api/family",
    "data":  {
                "username":"fagruddin",
                "password":"valaanur",
                "familyLeader":"fagruddin",
                "husband":"fagruddin",
                "wife":"rejiya",
                "child":2
     },
     "headers":{
         "Content-Type":"application/x-www-form-urlencoded"
     }
}

这里有什么问题?任何人帮我解决这个问题?

Live Demo for your reference

1 个答案:

答案 0 :(得分:2)

如果您使用快捷方式post方法,则省略配置参数,使第一个参数成为url。

由于您传入配置对象而不是url作为第一个参数,因此您收到错误。

$http.post(
  '/api/family', 
  vm.form, 
  {headers: {'Content-Type': 'application/x-www-formurlencoded'}}
).success(function(data) {
    console.log( 'data', data );
})

如果您使用的是直接http,则可以传递config对象:

$http({
    method: 'POST',
    url: 'api/family',
    data : vm.form,
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function(data) {
    console.log( 'data', data );
})