繁体   English   中英

MVC:使用 AJAX 从控制器获取 JSON 数据失败

[英]MVC: Get JSON data from a controller using AJAX failed

我正在尝试使用 AJAX 从控制器查询数据,我的控制器是:

[HttpGet]
public ActionResult GetTestStatus(string userName)
{
    var db = new SREvalEntities();
    var allTests = db.Tests
        .Where(x => string.Equals(x.Owner, userName))
        .Select(x => new { CreateDate = x.CreateDate, EndDate = x.EndDate, Status = x.Status })
        .OrderByDescending(x => x.CreateDate)
        .ToList();

    return Json(allTests, JsonRequestBehavior.AllowGet);
}

我在外部文件中的 javascript 代码是:

function Filter() {
    var userAlias = document.getElementById("UserAliasInput");
    var txt = "<tr><th>StartDate</th><th>EndDate</th><th>Status</th><th>Detail</th></tr>";
    $.ajax({
        url: '/TestStatus/GetTestStatus',
        type: "GET",
        dataType: "JSON",
        data: { userName: userAlias },
        success: function (results) {
            $.each(results, function (i, result) {
                txt += "<tr><td>" + result.CreateDate + "</td>";
                txt += "<td>" + result.EndDate + "</td>";
                txt += "<td>" + result.Status + "</td>";
                txt += "<td><a href=\"\">Goto</a></td></tr>";
            });
        }
    });
    $("#ShowDetail").html(txt);
}

当我试图调试这个函数时,代码永远不会执行到

$("#ShowDetail").html(txt);

我的页面永远不会改变。 我怎样才能让它工作? 谢谢。

当您使用$.ajax() ,它是异步的。 因此$("#ShowDetail").html(txt)在从 API 返回值之前被调用。

您应该在每个块之后设置html()

function Filter() {
    var userAlias = document.getElementById("UserAliasInput");
    var txt = "<tr><th>StartDate</th><th>EndDate</th><th>Status</th><th>Detail</th></tr>";
    $.ajax({
        url: '/TestStatus/GetTestStatus',
        type: "GET",
        dataType: "JSON",
        data: { userName: userAlias.value }, //Use the value property here
        success: function (results) {
            $.each(results, function (i, result) {
                txt += "<tr><td>" + result.CreateDate + "</td>";
                txt += "<td>" + result.EndDate + "</td>";
                txt += "<td>" + result.Status + "</td>";
                txt += "<td><a href=\"\">Goto</a></td></tr>";
            });

            $("#ShowDetail").html(txt); //Move code to set text here
        }
    });    
}

试试下面的功能

function Filter() {
    var userAlias = $("#UserAliasInput").val();//change this
    var txt = "<tr><th>StartDate</th><th>EndDate</th><th>Status</th><th>Detail</th></tr>";
    $.ajax({
        url: '/TestStatus/GetTestStatus',
        type: "GET",
        dataType: "JSON",
        data: { userName: userAlias },
        success: function (results) {
            $.each(results, function (i, result) {
                txt += "<tr><td>" + result.CreateDate + "</td>";
                txt += "<td>" + result.EndDate + "</td>";
                txt += "<td>" + result.Status + "</td>";
                txt += "<td><a href=\"\">Goto</a></td></tr>";
            });
            $("#ShowDetail").html(txt);//place this in the success function
        }
    });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM