我正在尝试使用 AJAX 调用来获取一些数据并通过 JSON 将多条数据返回给调用者。它适用于某些包含简单输出的测试。但是,当返回的元素之一是 HTML 时,它就不起作用。对此有什么想法吗?
// get_answer() pulls some HTML back from an XML document
$answer = $_SESSION['quiz_session']->get_answer();
// test output to make sure everything is working
echo $answer;
/** sample output **
*
<div>
<p>
<b>
<span class="gloss-def">a downward slope</span>
</b>
</p>
<p>Because the village was situated on the
<i>declivity</i> of a hill, it never flooded.
</p>
<p>
<i>Synonyms: decline; descent; grade; slant; tilt</i>
</p>
</div>
*
** end sample output **/
echo json_encode($answer);
// will output {}
最佳答案
尝试将 $answer 添加到 stdClass 或数组,例如
echo json_encode(array("answer"=>$answer));
关于PHP json_encode 函数返回空白 {},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20530369/