title: Laravel return rendered html view without \r \n -- json
body: <p>I have a little weird problem with rendering a view and replacing it after the ajax call with another html section.</p>
  
  <p>code looks like this:</p>
  
  <p>// ajax - controller function </p>
  
  <pre><code>$view = view('renderedData', [
  
              'test' =&gt; $data['test'],
              'unicorn' =&gt; $data['unicorn'],
          ])-&gt;render();
          return json_encode($view);
  </code></pre>
  
  <p>//  my ajax: </p>
  
  <pre><code>function sendData() {
          var test= "test data"
          var dummy = "unicorn whatever" 
          $.ajax({
              type: "post",
              url: "{{ route('ajaxRender') }}",
              data: {'test': test, 'dummy': dummy},
              success: function (data) {
                  $('tbody').replaceWith(data)
              }
          }); //end of ajax
      }
  </code></pre>
  
  <p>renderedData.blade view html: </p>
  
  <pre><code>&lt;tbody&gt;
  @foreach($data as $d)
          &lt;tr&gt;
              &lt;td&gt;{{ $c-&gt;name }}&lt;/td&gt;
              &lt;td&gt;{{ $c-&gt;$date }}&lt;/td&gt;
              &lt;td&gt;{{ $c-&gt;$height }}&lt;/td&gt;
          &lt;/tr&gt;
  @endforeach
  &lt;/tbody&gt;
  </code></pre>
  
  <p>( no whitespaces or empty lines at all in the blade ) </p>
  
  <p>The response in console looks like this:</p>
  
  <pre><code>"&lt;tbody&gt;\r\n                &lt;tr&gt;\r\n            &lt;td&gt;mydata&lt;\/td&gt;\r\n            &lt;td&gt;mydata&lt;\/td&gt;\r\n            &lt;td&gt;mydata&lt;\/td&gt;\r\n            &lt;td&gt;mydata&lt;\/td&gt;\r\n            &lt;td&gt;mydata&lt;\/td&gt;\r\n        &lt;\/tr&gt;\r\n                                            &lt;tr&gt;\r\n            &lt;td&gt;mydata&lt;\/td&gt;\r\n            &lt;td&gt;mydata&lt;\/td&gt;\r\n            &lt;td&gt;mydata&lt;\/td&gt;\r\n            &lt;td&gt;mydata&lt;\/td&gt;\r\n            &lt;td&gt;mydata&lt;\/td&gt;\r\n        &lt;\/tr&gt;\r\n    &lt;\/tbody&gt;"
  </code></pre>
  
  <p>I'm replacing the old tbody section with the new one. How ever, my table gets filled with my data and those  "\r \n &lt;/td>" and I don't know why. Like you can see in the picture below:</p>
  
  <p><a href="https://i.stack.imgur.com/sXL0D.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sXL0D.png" alt="enter image description here"></a></p>
  
  <p>Another thing I've found out in the web console is that the json html looks fine and the answer below do have those "\r \n ..." </p>
  
  <p>I think there is a problem with the "return json_encode($view);" because if I try a "return $view" - I can see the html formated correctly. </p>
  
  <p><a href="https://i.stack.imgur.com/B1nYS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/B1nYS.png" alt="enter image description here"></a></p>