500(内部服务器错误)Ajax调用Web.api Post方法

时间:2015-05-24 11:00:59

标签: jquery ajax asp.net-mvc asp.net-web-api

在我的“开始”视图中,我有一些按钮,客户需要选择他们的答案。它得到了一个叫做'#down;'显示他们已经按下了什么。之后他们可以按下一个按钮。它调用一个JQuery函数来获取类向下的按钮,用户ID和问题ID。

在此我调用一个ajax帖子,该帖子转到了值postcontroller到该post方法。

这是JQuery函数:

jQuery(function ($) {
var count = 0;
var answers;
var buttons = new Array();
var id = $(document.getElementsByName('id').valueOf());
var user = $(document.getElementsByName('user').valueOf());

$('#nextBtn').click(function ()
{
    $('.buttons').find('input[type="button"]').each(function ()
    {
        count++;
        buttons.push($(this).valueOf());
    });
    answers.push(id.attr('value'));
    answers.push(user.attr('value'));
    for (var i = 0; i < count; i++)
    {
        if (buttons[i].hasClass('down'))
        {
            answers.push(buttons[i].attr('value'));
        }
    }

    for (var i = 0; i < answers.length; i++) {
        console.log(answers[i]);
    }

       $.ajax({
                url: '/Home/Start/api/Values',
                type: 'POST',
        data: JSON.stringify(answers),
        contentType: "application/json;charset=utf-8",
        succes: function (data) {
            alert("worked!");
        },
        error: function (data) {
            alert("failed!");
        }
       });
    });
});

在valuescontroller中发布方法

 [HttpPost]
    [Route("Home/Start/api/values")]
    public void Post([FromBody]Object[] value)
    {

        convertToAnswer(value);

        //answerLength = _db.Answers.Count();

        //get last id in answers
        answerLength = (from a in _db.Answers
                    select a.Id).Max();

        saveAnswer(value.Length);

        finish(questionId);


      }

和start.cshtml

    <html lang="en">
     <head>
    <meta charset="utf-8">
    <title></title>
    <link rel="stylesheet"     href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
</head>
     <body>
       @{
          ViewBag.Title = "Enquête";
          int main = ViewBag.main;
          int extra = ViewBag.extra;
       }

       <div class="main">
        @foreach (var item in Model)
          {
           ViewBag.counter = @item.Id;
            var counter = @item.Id;


    <div class="container-fluid">
        <div class="jumbotron">
            <h1>@item.Category.Name </h1>

            <div class="progress " >
                <div class="progress-bar progress-bar-success" id="determinate"></div>
            </div>

            <script>
                updateProgress(@item.Id, @ViewBag.length);   //Update the progressbar
            </script>


            <p>@item.Text </p>
            <img src="data:image/jpg;base64,@item.Picture" alt="test" />
        </div>

        <script>
            assignAnswers(@main, @extra);
        </script>

        <form>
            <div class="buttons" >
                <div id="answers">
                    <input id="a1" type="button" class="button mainQuestion noExtraQuestion" value="Verloopt naar wens" />
                    <input id="a2" type="button" class="button mainQuestion noExtraQuestion" value="Probleem - &#x00A;niet hinderlijk" />
                    <input id="a3" type="button" class="button mainQuestion" value="Probleem - &#x00A;hinderlijk voor cliënt" />
                    <input id="a4" type="button" class="button mainQuestion" value="Probleem - &#x00A;hinderlijk voor mantelzorger" />
                    <input id="a5" type="button" class="button mainQuestion" value="Probleem - &#x00a;hinderlijk voor beide" />
                    <input name="id" type="hidden" value="@ViewBag.counter" />
                    <input name="user" type="hidden" value="@ViewBag.user"/>
                </div>
                <div class="buttons" id="extraAnswers">
                    <p>Wilt u hier hulp bij?</p>
                    <input id="eA1" type="button" class="z-depth-2 button extraQuestion" value="Ja" />
                    <input id="eA2" type="button" class="z-depth-2 button extraQuestion" value="Nee" />
                </div>

                <div id="navButtons">
                    <input type="button" id="prevBtn" class="navButton" value="Vorige" onclick="location.href='@Url.Action("Start", "Home", new { Id = ViewBag.counter - 1, userID = ViewBag.user })'" />
                    <input type="button" id="nextBtn" class="navButton disabled" value="Volgende" onclick="location.href='@Url.Action("Start", "Home", new { Id = ViewBag.counter + 1, userID = ViewBag.user })'" disabled /> <!--Standard disabled untill questions and-->
                </div>
            </div>
        </form>



    </div>
    <script>
        disablePrev(@item.Id);   //disable prev button if first question
    </script>
}
</div>
</body>
</html>

在当地一切正常,但现在它是在azure上发布的。我无法在数据库中向表中写入新条目。如果我回到之前填写的问题。它确实有效,我可以在数据库中覆盖它,这也适用于post方法。

这是我在浏览器中获得的错误消息(chrome)

POST http://hostingwebsite/Home/Start/api/Values 500 (Internal Server Error)
n.ajaxTransport.k.cors.a.crossDomain.send @ jquery-2.1.1.min.js:4
n.extend.ajax @ jquery-2.1.1.min.js:4
(anonymous function) @ functions.js:92  ==> this points to the ajax call
n.event.dispatch @ jquery-2.1.1.min.js:3
n.event.add.r.handle @ jquery-2.1.1.min.js:3

我希望这是关于我的问题的足够信息,这是我第一次在这里提问。

如果您需要更多信息/代码,请告诉我们。

0 个答案:

没有答案