Javascript 未使用.html()显示JSON数据
在发布这个问题之前,我在这里搜索了一下,得到了不同的答案,我认为它不适合我的需要。当我点击一个按钮时,下面的js脚本将运行Javascript 未使用.html()显示JSON数据,javascript,php,jquery,json,ajax,Javascript,Php,Jquery,Json,Ajax,在发布这个问题之前,我在这里搜索了一下,得到了不同的答案,我认为它不适合我的需要。当我点击一个按钮时,下面的js脚本将运行 $("#update-details-btn").on('click', function() { $.ajax({ type: 'post', dataType: 'json', data: "confirmation="+get_data, url: '../
$("#update-details-btn").on('click', function() {
$.ajax({
type: 'post',
dataType: 'json',
data: "confirmation="+get_data,
url: '../for_update_details',
success: function(data)
{
console.log(data);
$('div#update_details_body').html(data.results);
这是集装箱
<div id="update_details_body" class="modal-body"></div>
数据来自一个php函数
$data['results'] = NULL;
$results = "<div class='form-group'>";
$results .= "<label for='document-type' class='col-sm-4 control-label'>Category</label>";
$results .= "</div>";
$data['results'] = $results;
echo json_encode($data);
正如您从js中看到的,我做了一个console.log,它精确地打印data.results包含的内容,但是当我使用.html放入容器中时,什么也没有发生。罪魁祸首可能是什么?我正在对其他功能进行编码,但只有这一部分不起作用
var data = $.parseJSON(data);
将此添加到成功函数中。希望能有帮助 从您的示例中,我发现您不需要将数据解析为json。只需响应$results并绑定ajax成功
$("#update-details-btn").on('click', function() {
$.ajax({
type: 'post',
data: "confirmation="+get_data,
url: '../for_update_details',
success: function(data)
{
console.log(data);
$('div#update_details_body').html(data);
PHP:
在echo json_编码之前添加以下行:
header('Content-Type: application/json');
echo json_encode($data);
代码:
php代码非chnage可能是您需要在console.log之前解析JSON.parsedata。无需解析他有数据类型:“JSON”,您确定数据没有绑定到您的div吗,我认为问题在于class=modal body它隐藏的somewhere hi@Rahul,div是模态主体,单击按钮时会显示整个模态您是否尝试记录数据。结果而不仅仅是数据?有什么错误吗?嗨,我试试这个
header('Content-Type: application/json');
echo json_encode($data);
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#update-details-btn").on('click', function() {
alert("inside");
$.ajax({
type: 'post',
dataType: 'json',
url: 'php_file.php',
success: function(data)
{
console.log(data);
$('div#update_details_body').html(data.results);
}
});
});
});
</script>
</head>
<style>
.modal-body
{
margin: 0px;
padding: 0px;
width: 100px;
height: 100px;
}
</style>
<body>
<div id="update_details_body" class="modal-body"></div>
<input type="button" id="update-details-btn" name="button" value="button">
</body>
</html>