从视图JS调用Controller函数

时间:2014-11-15 09:34:11

标签: javascript asp.net-mvc-4 model-view-controller

MVC Noob在这里。

基本上,我正在尝试调用apicontroller中的函数。这是apicontroller方面:

[HttpPost]
    public HttpResponseMessage<String> Recognize(string strokes)
    {
        var strokePointsData = JsonConvert.DeserializeObject<dynamic>(strokes);

        var strokeCollection = GetStrokeCollectionFromPoints(strokePointsData);

        var inkAnalyzer = new InkAnalyzer();

        inkAnalyzer.AddStrokes(strokeCollection);

        var analysisStatus = inkAnalyzer.Analyze();

        if (analysisStatus.Successful)
        {
            var recognizedString = inkAnalyzer.GetRecognizedString();
            return new HttpResponseMessage<string>(recognizedString);
        }
        else
            return new HttpResponseMessage<string>("Data not recognized");
    }

这是JS:

$('#submitBtn').click(function () {
        $.post('/api/Recognition/Recognize',
               { strokes: JSON.stringify(strokesHistory)},
               function (result) {
                   alert(result);
               });
    });

然而,每次编译和运行时,都会收到"Failed to load resource: the server responded with a status of 500 (Internal Server Error) http://localhost:1409/api/recognition/recognize"错误消息。这告诉我它正在/ api / recognition / recognition中的根文件夹中查找一个对象,但我没有在根文件夹中看到任何这样的文件夹,所以这个错误是有道理的。然而,从我所做的所有研究中,看起来我上面的URI应该是正确的上下文。我错过了什么?

0 个答案:

没有答案