Symfonyde ajax ile veritabanından veri getirmeye çalışıyorum ama bir türlü yapamadım. Yapmak istediğim, kullanıcı profil sayfasına kullanıcının paylaştığı postları ajax ile getirmek.
profile.html.twig
{% extends 'base.html.twig' %} {% block title %}User{% endblock %} {% block body %} <div class="profile-lasts"></div> <script> $(document).ready(function () { $.ajax({ url: '{{ path('show_user_posts') }}', type: 'post', dataType: 'json', contentType: 'application/json', async: true, data: { 'userid': {{ app.user.id }} }, success: function(data) { $.each(data, function () { $(".profile-lasts").prepend("<table><tr><td>Username</td><td>Content</td><td>Creadted at</td></tr><tr><td>"+this.username+"</td><td>"+this.content+"</td><td>"+this.createdAt+"</td></tr></table>"); }); } }); }); </script> {% endblock %}
PostController.php
<?php namespace App\Controller; use App\Entity\Post; use App\Entity\User; use App\Form\PostType; use App\Repository\PostRepository; use App\Repository\UserRepository; use phpDocumentor\Reflection\Types\Array_; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Serializer\SerializerInterface; /** * @Route("/post") */ class PostController extends AbstractController { /** * @Route("/show", name="show_user_posts", methods={"GET","POST"}) */ public function show(Request $request, UserRepository $userRepository, PostRepository $postRepository, SerializerInterface $serializer): Response { if ($request->isXmlHttpRequest()){ $userid = $_POST['userid']; if (!empty($userid)){ $user = $userRepository->findBy(['id' => $userid]); $posts = $postRepository->findBy(['user' => $user[0]]); return new JsonResponse($posts); } return new JsonResponse('userid null'); } return new JsonResponse('not xmlhttprequest'); } }
Post.php
<?php namespace App\Entity; use Doctrine\ORM\Mapping as ORM; use phpDocumentor\Reflection\Types\Integer; /** * @ORM\Entity(repositoryClass="App\Repository\PostRepository") */ class Post { /** * @ORM\Id() * @ORM\GeneratedValue() * @ORM\Column(type="integer") */ private $id; /** * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="posts") */ private $user; /** * @ORM\Column(type="text", nullable=true) */ private $content; /** * @ORM\Column(type="string", length=75, nullable=true) */ private $image; /** * @ORM\Column(type="string", length=10, nullable=true) */ private $likecounter; /** * @ORM\Column(type="string", length=10, nullable=true) */ private $dislikecounter; /** * @ORM\Column(type="string", length=10, nullable=true) */ private $commentcounter; /** * @ORM\Column(type="string", length=10, nullable=true) */ private $status; /** * @ORM\Column(type="datetime", nullable=true) */ private $created_at; /** * @ORM\Column(type="datetime", nullable=true) */ private $updated_at;
Bir haftadır bu sorun ile uğraşıyorum, yardımcı olabilirseniz çok sevinirim. Mezun olmadan önce projemi bitirmek istiyorum.
Sorun ne, hata mı alıyorsun?
Failed to load resource: the server responded with a status of 500 ()
bu hatayı veriyor
Bence github'a at, public repo olarak, öyle bakalım. Bu şekilde debug edemeyiz.
tamam atıyorum.
aslında github kullanmayı bilmiyorum, denedim ama yapamadım.
hallettim sorunumu. teşekkür ederim ilgilendiğiniz için.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Symfonyde ajax ile veritabanından veri getirmeye çalışıyorum ama bir türlü yapamadım. Yapmak istediğim, kullanıcı profil sayfasına kullanıcının paylaştığı postları ajax ile getirmek.
profile.html.twig
PostController.php
Post.php
Bir haftadır bu sorun ile uğraşıyorum, yardımcı olabilirseniz çok sevinirim. Mezun olmadan önce projemi bitirmek istiyorum.
Sorun ne, hata mı alıyorsun?
Failed to load resource: the server responded with a status of 500 ()
bu hatayı veriyor
Bence github'a at, public repo olarak, öyle bakalım. Bu şekilde debug edemeyiz.
tamam atıyorum.
aslında github kullanmayı bilmiyorum, denedim ama yapamadım.
hallettim sorunumu. teşekkür ederim ilgilendiğiniz için.