0

Javascript:

$(function () {
    $("#btEmail").click(function () {
        var name = $("#username").val();

        $.post("@Url.Action("SearchUserByUsername", "LogonWorkFlow")", { username: name }, function (result) {
            var r = result;

        });
    });
});

Controller MVC:

[HttpPost]
        public ActionResult SearchUserByUsername(string username)
        {
            return Json(GetUserByEmail(username), JsonRequestBehavior.AllowGet);
        }

public async Task<JsonResult> GetUserByEmail(string email)
        {
            var u = await userManager.FindByEmailAsync(email);

            var user = new { mail = u.Email, n = u.FullName };

            return Json(user, JsonRequestBehavior.AllowGet);
        }

The request usually passes by function, but the return is always empty

1 Answer 1

2

You're not retuning a string, you're returning a task. Which the JSON serializer doesn't know what to do with and which has no meaningful string representation to return to the client. SearchUserByUsername should also be async. Something like:

public async Task<ActionResult> SearchUserByUsername(string username)
{
    return Json(await GetUserByEmail(username), JsonRequestBehavior.AllowGet);
}

That way it can await the call to GetUserByEmail, which is async.


Although since GetUserByEmail already returns a JsonResult you can (and probably should) simplify:

public async Task<ActionResult> SearchUserByUsername(string username)
{
    return await GetUserByEmail(username);
}

Of course, at this point it begs the question of why you really need the SearchUserByUsername operation in the first place, since it's just a pass-through to an existing operation.

Sign up to request clarification or add additional context in comments.

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.