0

when I try to get data in ajax, the returned object is empty

I send the id of the data I want to get in js :

    function selectMessage(id) {
         
        $.ajax({
                url: '{{ path('back_translation_update') }}',
                method: 'GET',
                data: {id: id}
        }).done(function (response) {
            console.log(response)
        })
         
    }
     
    $('.updateMessage').click(function (evt) {
        evt.stopPropagation()
        selectMessage($(this).data('id'))
    })

in the controller I look for the data to return :

    /**
     * @Route("/update", name="back_translation_update", methods="GET|POST")
     */
    public function getById(Request $request): Response
    {
        if ($request->isXMLHttpRequest()) {
             
            $id = $request->get('id');
//            dd($id);
             
            $message = $this->translationService->getTranslationById($id);
             
//            return new JsonResponse(['data' => $message]);
 
            $response = new Response();
            $response->setContent(json_encode([
                'data' => $message,
            ]));
            $response->headers->set('Content-Type', 'application/json');
            return $response;
        }
                 
    }

I use a service because with the repository I get an error: getById () must be an instance of Symfony\Component\HttpFoundation\Response

with :

$repositoryMessage = $this->em->getRepository(TranslationMessage::class); 
$message = $repositoryMessage->findOneBy(['id' => $id]);

so the service will look in the database:

public function getTranslationById($translation_id)
{
    $query = $this->em->createQueryBuilder()
        ->from(TranslationMessage::class,'message')
        ->select('message')
        ->where('message.id = ?1')
        ->setParameter(1, $translation_id);
     
    $message = $query->getQuery()->getResult();
//    dd($message);
    return $message;
}

all the dd() give the expected values:

  • into getById(): the id of the row sought

  • into getTranslationById(): the sought object

but in the XHR, data contains an empty object: uh:

same with a new JsonResponse, commented here

what did I miss? help

1 Answer 1

2

Use Aurowire to get messageRepository object and use $this->json() to return JsonResponse

/**
* @Route("/update", name="back_translation_update", methods="GET|POST")
*/
public function getById(Request $request, TranslationMessageRepository $messageRepository): JsonResponse
{        
    $id = $request->query->get('id');
    $message = $messageRepository->find($id);

    if(!$message) { return new NotFoundHttpException(); }

    return $this->json([
         'success' => true,
         'data' => $message
    ]);
                 
}

Define success function instead of done function

function selectMessage(id) {
        $.ajax({
                url: "{{ path('back_translation_update') }}",
                method: 'GET',
                data: { id: id }
                success: function(data) {
                    console.log(data)
                }

        })
         
}
Sign up to request clarification or add additional context in comments.

1 Comment

Great thanx I get it! I just have to adapt url path to get it from js file because there is locale parameter, so thanx one again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.