mercredi 21 septembre 2016

Ajax call to web api causes internal error 500

I am doing some simple application in ASP.NET I am calling by ajax web api to get some data in js inside view page. It doesn't work

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script src="~/Scripts/jquery.signalR-2.2.1.min.js"></script>
    <script src="/signalr/hubs"></script>

</head>
<body>

<div style="width: 50%; border: solid 1px Red; height: 40px">
    <h3 style="margin: 10px 0px 0px 10px">
        <span id="spnName"></span>
    </h3>
</div>
<div style="width: 50%; border: solid 1px Red; height: 100px">
    <div style="height: 70%" id="divChat"></div>
    <div style="border: dashed 1px Black;">
        <div style="float: left; width: 70%; padding: 4px">
            <input type="text" style="width: 100%" id="txtMsg"/>
        </div>
        <div style="float: right; width: 19%; padding: 4px">
            <input type="button" id="btnSend" value="Send Message"/>
        </div>
    </div>
</div>

<script type="text/javascript">

    $(document).ready(function() {

        var userFullName;
        $.ajax({
            method: "GET",
            url: "/api/user/getFullName",
            contentType: "application/json; charset=utf-8",
            success: function (result) {
                userFullName = result;        
            }
        });

        $("#spnName").text(userFullName);
        $("#txtMsg").val('');

        var chatProxy = $.connection.chatHub;

        $.connection.hub.start().done(function() {
            $("#btnSend").click(function() {
                chatProxy.server.broadCastMessage($("#spnName").text(), $("#txtMsg").val());
                $('#txtMsg').val('').focus();
            })
        })

        chatProxy.client.receiveMessage = function(msgFrom, msg) {
            $('#divChat').append(msgFrom + msg)
        };


    });
</script>

</body>
</html>




    [Route("api/user/getFullName")]
    [HttpGet]
    public IHttpActionResult GetUserFullName()
    {
        string fullName = _userService.GetCurrentUserFullName();
        return Ok(fullName);
    }

Why it gives me 500 internal error in F12 developer tools at calling : url: "/api/user/getUserFullName" ? jquery-1.10.2.min.js:23 GET http://localhost:55525/api/user/trial 500 (Internal Server Error)




Aucun commentaire:

Enregistrer un commentaire