AngularJs $http.post() does not send data

前端 未结 30 1832
我在风中等你
我在风中等你 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条回答
  •  梦毁少年i
    2020-11-22 03:38

    Unlike JQuery and for the sake of pedantry, Angular uses JSON format for POST data transfer from a client to the server (JQuery applies x-www-form-urlencoded presumably, although JQuery and Angular uses JSON for data imput). Therefore there are two parts of problem: in js client part and in your server part. So you need:

    1. put js Angular client part like this:

      $http({
      method: 'POST',
      url: 'request-url',
      data: {'message': 'Hello world'}
      });
      

    AND

    1. write in your server part to receive data from a client (if it is php).

              $data               = file_get_contents("php://input");
              $dataJsonDecode     = json_decode($data);
              $message            = $dataJsonDecode->message;
              echo $message;     //'Hello world'
      

    Note: $_POST will not work!

    The solution works for me fine, hopefully, and for you.

提交回复
热议问题