title: How to render json with Phoenix framework?
body: <p>If use Rails framework with ajax request, this way in controller can send data back to front-end:</p>
  
  <pre><code>def get_posts
    @posts = Post.where(user_id: params[:user_id].to_i)
    if request.xhr?
      render :json =&gt; {
        :posts =&gt; @posts
      }
    end
  end
  </code></pre>
  
  <p>Front-end can catch it:</p>
  
  <pre><code>$.ajax({
    type: "GET",
    url: "&lt;%= get_post_path %&gt;",
    data: {user_id: $("#user_id").val()},
    success: function(data, textStatus, xhr) {
      alert(data.posts);
      //...
    }
  </code></pre>
  
  <p>Then in Phoenix framework, how to send data back to front-end?</p>
  
  <p>I saw this guide from official docs:</p>
  
  <blockquote>
    <p><a href="https://hexdocs.pm/phoenix/views.html#rendering-json" rel="nofollow noreferrer">https://hexdocs.pm/phoenix/views.html#rendering-json</a></p>
  </blockquote>
  
  <p>It's method is using <code>render("page.json", %{page: page})</code> to send data but I don't want to create a json file.</p>
  
  <p>So is it possible to send data back to front-end like Rails way?</p>