A
A
Andrey2019-01-06 05:12:58
JavaScript
Andrey, 2019-01-06 05:12:58

Why is there no DB entry via AngularJS API and Symfony 4?

In general, there is a post request from Angular to Symfony with parameters. But there is an error "Call to a member function setName() on null". But if you run the request through the PhpStorm tool, everything works.
Method:

/**
     * @Route("/rest_api/update", methods={"POST"})
     */
    public function restApiUpdate(Request $request)
    {
        $id = $request->request->get('id');
        $user = $request->request->get('user');
        $user = urldecode($user);
        $em = $this->getDoctrine()->getManager();
        $modelUser = $em->getRepository(Users::class)->find((int)$id);
        $modelUser->setName($user);
        $em->flush();

        return new JsonResponse($id);
    }

AngularJS:
$http({method: 'POST', url: 'http://public/rest_api/update', params: {'id': id, 'user': user}}).
                then(function success (response) {
                    $scope.response=response.data;
                    console.log(user);
                });

The request itself: public/rest_api/update?id=4&user=%D0%93%D1%83%D1%8...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey, 2019-01-06
@DronTat

bears was right, we need to fix the request:

$http({method: "POST", url: "http://public/rest_api/update", headers: {'Content-Type': undefined}, data: {id: id, user: user}}).
                then(function success (response) {
                    $scope.response=response.data;
                    console.log(user);
                });

PHP:
$postdata = file_get_contents("php://input");
$data = json_decode($postdata);

B
bears, 2019-01-06
@bears

 id is passed through the get parameter
and in the code you get this id from the $_POST array and you need it from $_GET UPD: the same situation with the user

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question