5
Mam trochę PHP, który wyprowadza json.Wyjściowy kod HTML w ciągu JSON wygenerowany przez PHP
<?php
$html = utf8_encode($gegevens['tekst']);
$html = htmlentities($html);
//$html = htmlspecialchars($gegevens['tekst'], ENT_QUOTES, 'UTF-8');
echo json_encode(array('titel' => $gegevens['titel'], 'html' => $html));
?>
Wyjście będzie jak:
{"titel":"Here comes the title","html":"<strong>Here is the HTML<\/strong>\n<br \/>\n<br \/>\n And some more."}
a jQuery/Ajax będą:
$.ajax({
type: "GET",
url: "content/popup.php?id=" + id2,
dataType: 'json',
crossDomain: true,
success: function(json) {
var titel = json['titel'];
var html = json['html'];
function ContentTonen()
{
// Div's legen van content
$('.popup_home_head_inside').empty();
$('.popup_home_content_inside').empty();
$('.popup_home_head_inside').html(titel);
var html2 = html.replace(/\"/g, "");
//$('.popup_home_content_inside').html(html2);
$('.popup_home_content_inside').html(html2);
a wyjście HTML jest:
<strong>Some HTML</strong> <br /> Some more text.
więc nie będzie przetwarzane jako HTML.
Czy możesz mi pomóc?
Jak przygotować html dla JSON wtedy? Stripslashes nie działa. – Daan
Jakiego rodzaju przygotowań masz na myśli? – Jithesh
Po wstawieniu kodu HTML bezpośrednio do JsonEncode zwraca on wartość null. Ponieważ nie jest prawidłowy JSON. W przypadku htmlentities JsonEncode zaakceptował ciąg jako poprawny JSON. Ale której funkcji należy użyć do przygotowania kodu HTML, aby był prawidłowym JSON? – Daan