Maria Kari Maria Kari - 5 months ago 34
Javascript Question

How to JSON encode the data having HTML tags?

I have a products data with products information. It's actually in form of array but I tried to use

json_encode
function to convert it into JSON response.

The problem is it has some HTML code inside product description so it's showing some lines.

Link : http://superwholesalebazaar.xyz/wocom.php

Why it's showing the lines ? Isn't it the JSON response ?

Thanks!

Answer

Its showing the lines because in the HTML part of the data you have elements which naturally have the line strikethrough style associated with it such as the <del> element.

But since its a json response and you are sending it as a text/html type the browser renders it all as HTML and since json encode escapes some of the characters in the HTML its showing up a little weird such as some elements aren't closed properly.

I think what you want to do is send this response with the content type set to "json"

Here is how you can do this in php:

header('Content-Type: application/json');
// echo your json content here
Comments