Web API在正文中返回空结果

时间:2017-06-21 20:10:49

标签: c# asp.net-web-api2

我有一个Web API函数,它返回当前用户的Facebook Id,我从会话中获取Id。 API函数正确显示id,但是当我通过JQuery函数连接到它时,它重新运行空字符串,当我在浏览器中尝试Web API函数时,我可以正确地看到结果但是当我打开源页面时我只能看到空字符串像这样: enter image description here

enter image description here

这是我的功能代码:

public class successController : ApiController
{
    [HttpGet]
    [Route("api/success")]
    public string success()
    {
        string val = "";
        if (System.Web.HttpContext.Current.Session["user"] != null)
        {
            val = System.Web.HttpContext.Current.Session["user"].ToString();
            return val;
        }
        else
            return "";
    }
}

2 个答案:

答案 0 :(得分:0)

你确定jquery调用使用get方法或帖子吗?。

您可以像fiddler一样安装网络嗅探器来检查每次通话之间的差异。

答案 1 :(得分:0)

对于相同的代码和逻辑,它对我有用。我没有检查用户会话,而是返回一个硬编码的字符串。

        public class SuccessController : ApiController {
    [HttpGet]
    [Route("api/success")]
    public string success() {
        return "2321312312312";
    }
}

}

以下是我的Jquery代码

 <script>
    $(function () {
        $.get("http://localhost:2000/api/success", function (response) {
            console.log(response); // server response
        });
    });

</script>

Screenshot