1

I'm trying to query data using AJAX from a controller, my controller is:

[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);
}

And my javascript code in an extern file is:

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);
}

When I tried to debug this function, the code will never excute to

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

And my page will never be changed. How can I get it work? Thanks.

2 Answers 2

1

As you are using $.ajax(), which is asynchronous. Thus the $("#ShowDetail").html(txt) is called before the value is returned from API.

You should set the html() after the each block

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
        }
    });    
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. I do as what you said but it doesn't work. I add alert(results); in success callback and the page gives no response. By typing /TestStatus/GetTestStaus?userName=... I can see the JSON returned by the controller.
@OnceJune, Use { userName: userAlias.value }
sorry to bother you again. Since '$.ajax()' is asynchronous, how can I do something after '$.ajax()' returned in the same function? Such as update the page? Thank you.
@OnceJune, Yes you can
0

Try the following function

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
        }
    });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.