AJAX调用不适用于控制器操作

时间:2015-09-08 06:23:58

标签: javascript jquery ajax asp.net-mvc sharepoint

Salaamun Alekum

我的AJAX调用未在ASP.NET MVC Web Applicaiton项目中调用Controller操作

Bellow是我的AJAX调用Javascript而下一个是控制器的行动

AJAX致电

    var requestUrl = '/Home/GetCurrentUser';
    $.ajax({
        url: requestUrl,
        type: 'GET',
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        success: function(data)
        {
            debugger;
            alert(data);
        },
        error: function (xhr, status, error)
        {
            debugger;
            alert(error);
        }

控制器动作

[SharePointContextFilter]
        public JsonResult GetCurrentUser()
        {
            CurrentUserModel um = new CurrentUserModel();
            try
            {
                Microsoft.SharePoint.Client.User spUser = null;
                var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);
                using (var clientContext = spContext.CreateUserClientContextForSPHost())
                {
                    if (clientContext != null)
                    {
                        spUser = clientContext.Web.CurrentUser;

                        clientContext.Load(spUser, user => user.Title, user => user.Email, user => user.LoginName);

                        clientContext.ExecuteQuery();

                        um.Name = spUser.Title;
                        um.Email = spUser.Email;
                        um.LoginName = spUser.LoginName;
                    }
                }

                SharePointBoxOnline.Common.User u = UserManager.Instance.GetUserByEmail(um.Email);

                if (u != null)
                {
                    um.ClientId = u.FK_Client_ID;
                    um.UserId = u.User_ID;
                }
            }
            catch (Exception e)
            {
                SharePointBoxOnlineAppWeb.Classes.LogsManager.LogException(e.Message, e.StackTrace, System.Web.HttpContext.Current.Request.Url.ToString(), "Added logging functionality to store the exception information in the Database", DateTime.Now);
            }

            return Json(um, JsonRequestBehavior.AllowGet);
        }

AJAX中的错误结果

error.description

  

无效字符

状态

  

parsererror

xhr.responseText

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Error</title>
    <link href="/Content/css?v=MDbdFKJHBa_ctS5x4He1bMV0_RjRq8jpcIAvPpKiN6U1" rel="stylesheet"/>

</head>
<body>
    <div class="container">
        <div class="jumbotron">
            <h2>An unexpected error has occurred.</h2>
            <p>Please try again by launching the app installed on your site.</p>
        </div>
    </div>

<!-- Visual Studio Browser Link -->
<script type="application/json" id="__browserLink_initializationData">
    {"appName":"Internet Explorer","requestId":"673b269bf2c74e39a9496d69f3e0b62e"}
</script>
<script type="text/javascript" src="http://localhost:14069/4b2e31c8e2cf413facce9558ed0cb3ff/browserLink" async="async"></script>
<!-- End Browser Link -->

</body>
</html>

谢谢Stackoverflow和Stackoverflow的成员请让我知道如果您需要更多详细信息

谢谢

1 个答案:

答案 0 :(得分:2)

$.ajax({
    url: requestUrl,
    type: 'GET',
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',
    success: function(data)
    {
        debugger;
        alert(data);
    },
    error: function (xhr, status, error)
    {
        debugger;
        alert(error);
    }
});