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

Display data into to specific element using Ajax Get method

本文关键字:元素 显示 数据 Ajax Get 方法 使用      更新时间:2023-09-26

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

数据


id全名dob
101 John Doe 2015年3月1日
102 Mary Ann 2010年8月5日

HTML

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

Javascript

当前页面url为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);

像这样尝试

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