显示我从ajax获取的数据

时间:2017-06-07 08:04:12

标签: jquery ajax laravel bootstrap-modal

这是我想要显示从ajax数据中获取的值的模态,但它不起作用。我在span元素上有id="view_name"。我不知道它不会出现什么问题

这是触发fun_view功能的代码:

<td>
    <a href="" data-id="" class="display" data-toggle="modal" data-target="#myModal" onclick="fun_view('{{ $dat->id }},{{ $dat->name }},{{ $dat->company_name }}, {{ $dat->mobile_phone }}, {{ $dat->timezone }}, {{ $dat->best_time_to_call }}')"><?php echo $dat->name;?></a>
</td>

<div class="modal-body">         
    <p><b>Name : </b><span id="view_name" class="text-success"></span></p>
    <p><b>Company Name : </b><span id="view_company_name" class="text-success"></span></p>
    <p><b>Mobile Phone : </b><span id="view_mobile" class="text-success"></span></p>
</div>

<script>        
function fun_view(id,name,company_name,mobile_phone,timezone,best_time_to_call)
{
    var view_url = $("#hidden_view").val();

    $.ajax({
        url: view_url,
        type:"GET", 
        data:
        {
            "id": id,
            "name": name,
            "company_name": company_name,
            "mobile_phone": mobile_phone,
            "timezone": timezone,
            "best_time_to_call": best_time_to_call
        },
        success: function(result){
          //when I alert(result.name) it says undefined
          $("#view_name").text(result.name);
          $("#view_company_name").text(result.company_name);
          $("#view_mobile").text(result.mobile_phone);
        }
    });
}
</script>

2 个答案:

答案 0 :(得分:0)

添加数据类型只是为了确保ajax响应在JSON对象中  而且我想知道选择器$('#hidden_​​view')也许你有一些隐藏它的CSS。

$。AJAX({

    url: view_url,
    type:"GET",
    datatype: 'JSON',
    data: {"id":id, "name":name, "company_name":company_name, "mobile_phone":mobile_phone, "timezone":timezone, "best_time_to_call":best_time_to_call },
    success: function(result){
      //when i alert(result.name) it says undefined
      $("#view_name").text(result.name);
      $("#view_company_name").text(result.company_name);
      $("#view_mobile").text(result.mobile_phone);
    }

});

尝试以上代码

答案 1 :(得分:-1)

您可能忘记包含对JQuery脚本的引用。

<script
  src="https://code.jquery.com/jquery-3.2.1.min.js"
  integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
  crossorigin="anonymous"></script>