返回带有 HTML 字符串的数组到 jquery $.post 方法
Return array with HTML string to jquery $.post method
>我正在使用jquery $.post方法调用一个php脚本,作为
$.post('{$site_url}tests/tests/content', params, function (data)
{
$('#some-div1').html(data[1]);
$('#some-div2').html(data[2]);
}
在 php 脚本中,我返回以下数组:
$return_html = array();
$return_html['key1'] = '<tr><td colspan="20" class="center">record 1</td></tr><tr><td colspan="20" class="center">record 2</td></tr>';
$return_html['key2'] = '<tr><td colspan="20" class="center">category 1</td></tr><tr><td colspan="20" class="center">category 2</td></tr>';
echo json_encode($return_html);
exit;
返回的 json 字符串在我的页面上未正确显示 html。如何将其解析回 html 页面上的有效 html
你必须在
javascript中解析JSON数组才能获得其中包含的值。我建议尝试一下:
$.post('{$site_url}tests/tests/content', params, function (data)
{
var parsedData = JSON.parse(data);
console.log(parsedData);
}
这会将数据对象放入控制台,以便查看其组成方式。您应该能够使用 parsedData.key1 和 parsedData.key2 访问返回的数据。
干杯!
相关文章:
- Jquery POST未填充数组
- 防止双击执行两次jQuery post请求
- Jquery post and onclick
- jQuery$.post TypeError:e.type不是函数
- jQuery$.post可以在chrome、safari中工作,但不能在FF中工作(声明成功回调函数未定义)
- jQuery post,访问responseType或回调中的响应
- jquery post.fail,即使php成功
- 用jquery post发送jquery对象
- JQuery $.post() and curl
- 如何在 jquery post request 中发送api_key和api_secret参数
- Jquery Post Empty即使发送变量有数据
- 将 throbber 添加到 jQuery post()
- JQuery$.post第二次点击不起作用
- Ajax jquery post发布时没有变量名
- 404调用jQuery.post时出错
- Jquery Post方法在本地工作,但在服务器上不工作
- 从aspx.cs页面获取jquery post中的Null响应而不是json
- jQuery POST to PHP显示在浏览器中,但不显示在PHP文件中
- jquery $.post explain
- Jquery POST unicode字符串到codeigniter php