包含html的ajax json响应



发送前是否需要对ajax响应数据进行json_encode?

$html1 = '<span>some html</span>';
$html2 = '<span>some html</span>';
$res = array('html1'=>$html1, 'html2'=>$html2);
echo json_encode($res);

echo $res;

AJAX中的X代表XML

如果返回XHTML,则不需要对其进行json_encode

echo '<div>'.$html1.$html2.'</div>';

你可以

  1. 将其转储到页面someElement.innerHTML = returnValue;document.body.insertAdjacentHTML('afterend',returnValue);

  2. 从DOMFragment提取:


const domFragMent = document.createElement("div")
domFragment.innerHTML = returnValue;
const spans = domFragment.querySelectorAll("span");
  1. 使用domParser

  2. 发送一个json_encoded值数组,并在客户端上将其解析为跨度

最新更新