JQuery method .html() doesn't add the full HTML content from JSON response
I have some HTML generated by php using `json_encode()`, which look like this:
- ob_start();
- if ($my_query->have_posts()) :
- while ($my_query->have_posts()) : $my_query->the_post();
- get_template_part( 'template-parts/content', 'test' );
- endwhile;
- endif;
- $response['content'] = ob_get_clean();
- die(json_encode(utf8_encode($response)));
- }
This is how look the html from the php (wordpress posts):
- <article id="post-4996" class="post-4996 post type-post status-publish format-standard hentry category-news tag-dog">
- <header class="entry-header">
- TEST
- <a href="http://example.com/test/news/4996" rel="bookmark">Mon post</a>
- <div>TAGS:<a href="http://example.com/tag/dog" rel="tag">dog</a></div>
- </header>
- </article>
So with the `json_encode()`. I get the full html output in my JSON reponse, like that.
- {
- "content":"\n<article id=\"post-4996\" class=\"post-4996 type-post status-publish format-standard hentry category-news tag-dog\">\n\t<header class=\"entry-header\">\n\t\t\t\n\t\tTEST\n\t\t\t\t<a href=\"http:\/\/example.com\/test\/news\/4996\" rel=\"bookmark\">Mon post<\/a><div>TAGS:<a href=\"http:\/\/example.com\/tag\/dog\" rel=\"tag\">dog<\/a><\/div>\n\t\t\t<\/header><\/article>\n"
- }
And this is how i retrieve it in my jquery:
- var $container = $('#main');
- var $pagePosts = $container.find('article');
- var $myContainer = $('.my_container');
- function get_posts($params){
- jQuery.ajax({
- url: mdu.ajax_url,
- data: {
- action: 'my_get_posts',
- nonce: mdu.nonce,
- params: $params
- },
- type: 'POST',
- dataType:'json',
- success: function(data, textStatus, XMLHttpRequest {
- console.log('Success ', data.content);
- $pagePosts.empty();
- $myContainer.html(data.content);
- }
- });
- }
- $("a[rel='tag']").click(function(tag){
- event.preventDefault();
- clickTag = $(tag.target).text();
- $params = {'tag' : clickTag,};
- get_posts($params);
- });
I add the content using the JQUERY `.html()` method but on it only add the `<article>` tags and not the content (`<header>`,`<a>`,`<div>`,...) which means i have only this as a result in my web pageā¦
- <article id="post-4996" class="post-4996 post type-post status-publish format-standard hentry category-news tag-dog">
- </article>
I have check the JSON on jsonlint.com and it's clean, the JSON response correspond to what's expected only the `.html()` seems to be the issue but i don't get why.
Any input much appreciated.