使用Slim Framework从axios POST请求获取正文

时间:2018-09-10 10:08:37

标签: reactjs axios slim

嗨,伙计们在薄型框架3中无法获得身体形状的价值。

我目前正在尝试在我的react.js前端中使用axios

这是slim3路由文件: task.php

    <?php
    use Slim\Http\Request;
    use Slim\Http\Response;
    use \Firebase\JWT\JWT;

    $app->post('/task/add', function(Request $request,Response $response){

       // parse body
       $parsedBody = $request->getParsedBody();

       // data for insert
       $data = [
        ":TASK_STATUS" => 1,
        ":IDUSRN" => 1,
        ":TASK_FOR" => 16,
        ":TASK_DATE_START" => date('Y-m-d H:i:s'),
        ":TASK_DATE_END" => date('Y-m-d H:i:s'),
        ":TASK_SUBJECT" => $parsedBody['task_subject'],
        ":TASK_DETAIL" => $parsedBody['task_detail']
       ];


$sql = "INSERT INTO task (TASK_STATUS,IDUSRN,TASK_FOR,TASK_DATE_START,TASK_DATE_END,TASK_SUBJECT,TASK_DETAIL
        ) VALUES (:TASK_STATUS,:IDUSRN,:TASK_FOR,:TASK_DATE_START,:TASK_DATE_END,:TASK_SUBJECT,:TASK_DETAIL)";

        $stmt = $this->db->prepare($sql);

        try{
            $stmt->execute($data);
            return $response->withStatus(200)->withHeader("Content-Type", "application/json")->withJson(["status"=>"success","message"=>"Task successfully added !"]);

        }catch(PDOException $e){
            return $response->withStatus(401)->withHeader("Content-Type", "application/json")->withJson(["status"=>"Gagal","message"=>"Data tidak bisa terinput !"]);
        }
    });

我可以看到数据已成功插入。但是我收到的任务主题和任务详细信息为

这是react.js发布请求处理程序:

saveTask(e){
        e.preventDefault();

        const state = this.state;
        this.setState({
            addTask_status:false
        });

        const body = {                         
            task_subject: state.task_subject.value,
            task_detail: state.task_detail.value
        };

        axios({
          method: 'post',
          url: 'http://localhost:8080/task/add',
          headers:{
            "Content-Type": "application/json"
          },
          body
        }).then(function(response){
            console.log(response);
        })
        .catch((err)=>console.log(err));
    }

感谢您的帮助。

0 个答案:

没有答案