jQuery parse HTML

good afternoon and happy coding with coffee, I want to parse content article to view look beatifull.
While entry content article I’m used jQuery WySiWyG Simditor and successfull saved them.

below sample code to parser HTML code

  1. in Controller
    public function view($id = null)
    {
        $article = $this->Articles->get($id, [
            'contain' => ['Users', 'Comments', 'Tags']
        ]);
		$parsearticle = $this->Articles->find()->extract('Isi');
		return $this->response
			->withType('application/json')
			->withStringBody(json_encode([
				'parseHTML' => $parsearticle,
			]));

        $this->set('article', $article);
    }
  1. in table method
public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $options)
  {
     foreach($data as $key => $article) {
			if(is_string($article)) {
				$data[$key] = trim(stripslashes($article));
			}
	}
   }
  1. in ctp
    i’ve read tutorial based on
    https://discourse.cakephp.org/t/how-to-make-simple-jquery-ajax-in-cakephp-3-7/5834/2
    and also read in jQuery API documentation
    https://api.jquery.com/jquery.parsehtml/
<div id="parseHTML">
</div>

<script type="text/javascript">
 jQuery(document).ready(function() {
   var tampil = $("#parseHTML");
   var parse = '<?= Router::url(["controller" => "articles","action" => "view"]); ?>';
   var konv = $.parseHTML(parse);
   
   $.ajax({
      type:'post',
	  url:parse,
	  data:'&type=parseHTML',
	  dataType:'json',
	  success:function(){
		tampil.append(konv);
	});
  });
</script>

The output that I’m expect to look beauty article beelow this which content HTML code to formated layout pargraf, indentation and more

the result in view ctp all content JSON formated, below this

I hope someone help me to fix this, thanx verry much and happy coding

Are you trying to implement a live search?

no I didn’t.

in view.ctp
the logic tha’t I’m expect is, value of variable $article->Isi
send to jQuery to parse HTML, I have read tutorial jQuery API below
https://api.jquery.com/jquery.parsehtml/

and inside Article controller
to convert $article->Isi to JSON

public function view($id = null)
    {
        $article = $this->Articles->get($id, [
            'contain' => ['Users', 'Comments', 'Tags']
        ]);
		$parsearticle = $this->Articles->find()->extract('Isi');
		return $this->response
			->withType('application/json')
			->withStringBody(json_encode([
				'parseHTML' => $parsearticle,
			]));

        $this->set('article', $article);
    }

the end of view article is full JSON, It’s does not I’m expect, I want to view article for easy to reading, there is format identation, paragraf, align justify, bold, underline

as far as i understand, your jquery script was ment to do a live search from a database.
i think, you don-t need id.

did you try the following:
controller:

public function view($id = null)
    {
        $article = $this->Articles->get($id, [
            'contain' => ['Users', 'Comments', 'Tags']
        ]);
		$parsearticle = $this->Articles->find()->extract('Isi');


        $this->set('article', $parseartice);
    }

in view:

<php echo h($article); ?>