`$http:badreq Bad Request Configuration` - from angular post method, what is wrong here?

纵然是瞬间 提交于 2019-12-23 02:02:03

问题


I am developing sample app to learn the angularjs using node.js. when i post the data to backend to create a new family i am getting error as :

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"
     }
}

what is wrong here? any one help me to solve this?

Live Demo for your reference


回答1:


If you are using the shortcut post method, you omit the configuration parameter making the first parameter the url.

Since you passed in a configuration object instead of the url as the first parameter, you are getting the error.

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

if you are using the straight http, then you can pass config object :

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


来源:https://stackoverflow.com/questions/39432314/httpbadreq-bad-request-configuration-from-angular-post-method-what-is-wro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!