javascript - 使用 Ajax Get 方法将数据显示到特定元素

标签 javascript jquery ajax asp.net-mvc-5

我正在尝试将数据显示到 View 中。我运行开发人员工具,它说 id、fullName 和 dob 未定义。这些是数据库中的字段名称。我不确定我做错了什么,请纠正我。谢谢

数据


id fullName dob
101 John Doe 3/1/2015
102 Mary Ann 5/8/2010

HTML

<div>
      <p id="txtId"></p>
      <p id="txtName"></p>
      <p id ="txtDob"></p> 
</div>

Javascript

当前页面地址为http://localhost:53327/Records?customerName=DoeJohn&customerId=101

变量“id”从url(即101)获取参数,然后传递到ajax url:“/api/CustomerInfoApi”+ id,

var id = location.search.split('customerId=')[1];
    $.ajax({
        type: "GET",
        url: "/api/CustomerInfoApi/" + id,
        dataType: "json",
        success: function (data) {
            debugger;
            $("#txtId").text = data.customerId;
            $("#txtName").text = data.customerName;
            $("#txtDob").text = data.customerDob;
        }
    });

最佳答案

.text() 是一个函数。要设置元素的文本,您必须调用它并将文本作为参数传递给它

        $("#txtId").text(data.customerId);
        $("#txtName").text(data.customerName);
        $("#txtDob").text(data.customerDob);

关于javascript - 使用 Ajax Get 方法将数据显示到特定元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29244620/

相关文章:

javascript - 正则表达式替换多个字符

javascript - 在 ajax json 脚本中添加基本的 http 授权

javascript - 我将如何等待一系列异步任务在js中完成?

php - 如何使用 jquery ajax 将 anchor 的值发送到 php

jquery - 我怎样才能先 fadeIn() 然后 fadeOut() 然后 insertAfter()?

javascript - 如何使用React js在onchange中将数据从一个组件传递到另一个组件

javascript - translate3d() 导致 jQuery 悬停/单击事件无法正确触发

javascript - 部分叠加在另一个上

jquery - asp.net MVC4 中的全局化

javascript - ajax 调用缺少参数