AngularJs $http.post() does not send data

前端 未结 30 1777
我在风中等你
我在风中等你 2020-11-22 02:51

Could anyone tell me why the following statement does not send the post data to the designated url? The url is called but on the server when I print $_POST - I get an empty

30条回答
  •  [愿得一人]
    2020-11-22 03:21

    I had the same problem with AngularJS and Node.js + Express 4 + Router

    Router expects the data from post's request in body. This body was always empty if i followed the example from Angular Docs

    Notation 1

    $http.post('/someUrl', {msg:'hello word!'})
    

    But if i used it in the data

    Notation 2

    $http({
           withCredentials: false,
           method: 'post',
           url: yourUrl,
           headers: {'Content-Type': 'application/x-www-form-urlencoded'},
           data: postData
     });
    

    Edit 1:

    Otherwise node.js router will expect the data in req.body if used notation 1:

    req.body.msg
    

    Which also sends the information as JSON payload. This is better in some cases where you have arrays in your json and x-www-form-urlencoded will give some problems.

    it worked. Hope it helps.

提交回复
热议问题