为什么我的REST方法没有被这个jQuery调用?

时间:2016-09-02 16:45:05

标签: c# jquery ajax rest controller

我有一个类似的问题,关于jQuery按钮点击处理程序代码根本没有被解雇here

在这种情况下,被触发(当jQuery被添加到静态页面(Index.cshtml)时),但我的REST方法仍未被访问。控制台消息解释原因是:

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 

Requested URL: /LandingPage/GetQuadrantData

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.6.1055.0

这是jQuery,当单击按钮时确实会被触发,但是没有进行REST调用:

<script>
    $(function () {
        var btnGetData = document.getElementById('btnGetData');
        btnGetData.addEventListener("click", function () {
            alert("It works");
            var unitval = 'ABUELOS';
            var begdateval = '2016-08-07';
            var enddateval = '2016-08-13';

            $.ajax({
                type: 'GET',
                url: '@Url.Action("GetQuadrantData", "LandingPage")',
                data: { unit: unitval, begdate: begdateval, enddate: enddateval },
                contentType: 'application/json',
                cache: false,
                success: function (returneddata) {
                },
                error: function () {
                    alert('hey, boo-boo!');
                }
            });
        });
    });
</script>

REST方法在LandingPageController.cs中设置如下:

[Route("{unit}/{begdate}/{enddate}")]
public HttpResponseMessage GetQuadrantData(string unit, string begdate, string enddate)
{
    _unit = unit;
    _beginDate = begdate;
    _endDate = enddate;
    . . .

我在“_unit = unit”上有一个断点线,但没有到达;为什么不?我在这里缺少什么?

正在讨论的REST控制器类开始如下:

[RoutePrefix("api")]
public class LandingPageController : ApiController

注意:如果我在jQuery(附加的“Controller”)中使用它:

url: '@Url.Action("GetQuadrantData", "LandingPageController")'

... IDE(Visual Studio)编辑器中的“GetQuadrantData”和“LandingPageController”均为红色 - 字母为红色。

OTOH,如果我使用它(没有附加“控制器”):

url: '@Url.Action("GetQuadrantData", "LandingPage")'

(过去曾为我工作过,将“Controller”保留在Controller名称之外),“GetQuadrantData”和“LandingPage”都以红色显示,但字体仍为正常颜色。

更新

在下面的评论中使用Nico的链接,我将jQuery“url”行更改为:

url: '@Url.Action("GetQuadrantData", "LandingPage", new { httproute = "" })',

...但它仍然没有导致达到Controller方法。

更新2

单步浏览(Chrome)浏览器中的javascript,我发现“url”行已经从设计时的动态变化:

url: '@Url.Action("GetQuadrantData", "LandingPage", new { httproute = "" })',

...为:

url: '/api/LandingPage?action=GetQuadrantData',

我认为应该解决的问题更像是:

url: '/api/ABUELOS/2016-08-14/2016-08-20',

我是对的吗?为什么不这样解决?

如果我在浏览器中手动输入该URL,以便URL栏显示“http://localhost:52194/api/ABUELOS/2016-08-21/2016-08-27”它可以正常工作 - 该方法已达到并且“完成了它。”

更新3

我也尝试了这个:

$(function () {
    $("#btnGetData").click(function () {
        document.body.style.cursor = 'wait';
        $.ajax({
            type: "GET",
            url: '@Url.Action("GetQuadrantData", "LandingPage")',
            success: function (retval) {
                $("body").append($(retval));
                document.body.style.cursor = 'pointer';
            },
            error: function () {
                alert('error in btnGetData');
            }
        }); // end AJAX
    }); // end click
}); // end ready function

...但只看到“btnGetData中的错误”

更新4

我最近的失败是:

$("#btnGetData").click(function () {
    document.body.style.cursor = 'wait';
    var unitval = $('#unitName').val();
    var begdateval = $('#datepickerFrom').val();
    var enddateval = $('#datepickerTo').val();
    $.ajax({
        type: 'GET',
        url: '@Url.Action("GetQuadrantData", "LandingPage")',
        data: { unit: unitval, begdate: begdateval, enddate: enddateval },
        cache: false,
        success: function (returneddata) {
            alert($(returneddata));
        },
        error: function () {
            alert('error in ajax');
        }
    });
});

同样,我只看到“ajax中的错误”

更新5

注意:最高奖金(200分,我认为是)将奖励给能够解决这个难题的人。如果不止一个人这样做,那么赏金就会归于最能解决问题的人(这对我来说意味着最简单易行的方式。

为了完全披露和绝望,这里是WEB API应用程序运行时显示的第一页的全部内容(带有省略/无意义的部分省略省略号)(来自\ Views \ Home \ Index.cshtml :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>eServices Reporting - Customer Dashboard</title>
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">

    <!-- jQuery library -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

    <!-- Latest compiled JavaScript -->
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <!--[if IE]>
        <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->

    <style>
        body {
            padding-top: 20px;
            padding-bottom: 20px;
            background-color: white;
        }
    . . .
    </style>
    <script>
    $(function () {
        $("#btnGetData").click(function () {
            document.body.style.cursor = 'wait';
            var unitval = "ABUELOS"; //$('#unitName').val();
            var begdateval = $('#datepickerFrom').val();
            var enddateval = $('#datepickerTo').val();
            $.ajax({
                type: 'GET',
                url: '@Url.Action("GetQuadrantData", "LandingPage")',
                data: { unit: unitval, begdate: begdateval, enddate: enddateval },
                cache: false,
                success: function (returneddata) {
                    alert($(returneddata));
                },
                error: function () {
                    alert('error in ajax');
                }
            });
        });

    }); // end ready function
    </script>
</head>

<body>
    <div class="container body-content">
        <div class="jumbotronjr">
            <div class="col-md-3" style="margin-top: 0.6cm">
                <img src="http://www.proactusa.com/wp-content/themes/proact/images/pa_logo_notag.png" height="86" width="133" alt="PRO*ACT usa logo">
            </div>
            <div class="col-md-9">
                <label class="titletext" style="margin-top: 0.2cm;">Customer Dashboard</label>
                <br />
                <label class="titletextjr" style="margin-top: -2.2cm;" id="unitName">Craftworks</label>
                <label class="cccsfont"> for the week of August 14          </label>
                <input class="smalldatepicker" type="date" id="datepickerFrom" name="daterangefrom" value="2016-08-14">
                </input>
                <label class="cccsfont"> to </label>
                <input type="date" class="smalldatepicker" id="datepickerTo" name="daterangeto" value="2016-08-20">
                </input>
                <button class="btn green smallbutton" id="btnGetData" name="btnGetData">SUBMIT</button>
            </div>
        </div>    

        <div class="row">
            <div class="col-md-12">
                <hr />
            </div>
        </div>    

        <div class="row">
            <div class="col-md-12">
            </div>
        </div>

        <div class="row">
            <div class="col-md-6">
                <div class="topleft">
                    <h2 class="sectiontext">Top 10 Items Purchased</h2>

                    <table>
                        <tr>
                            <th>Item Code</th>
                            <th>Description</th>
                            <th class="rightjustifytext">Qty</th>
                        </tr>
                        <tr>
                            <td>101200</td>
                            <td>ASPARAGUS, STANDARD 11/1#</td>
                            <td class="rightjustifytext">32</td>
                        </tr>
                        <tr>
                            <td>140200</td>
                            <td>MUSHROOMS, MEDIUM 10#</td>
                            <td class="rightjustifytext">20</td>
                        </tr>
                        <tr>
                            <td>140000</td>
                            <td>MUSHROOMS, BUTTON 10#</td>
                            <td class="rightjustifytext">14</td>
                        </tr>
                        <tr>
                            <td>127100</td>
                            <td>LETTUCE, ROMAINE 24 CT </td>
                            <td class="rightjustifytext">14</td>
                        </tr>
                        <tr>
                            <td>300123</td>
                            <td>BEANS, GREEN TRIM 2/5# (BAGS)</td>
                            <td class="rightjustifytext">13</td>
                        </tr>
                        <tr>
                            <td>173100</td>
                            <td>POTATOES,  50 CT IDAHO</td>
                            <td class="rightjustifytext">12</td>
                        </tr>
                        <tr>
                            <td>234225</td>
                            <td>BERRIES, STRAWBERRY 1# CLAM</td>
                            <td class="rightjustifytext">11</td>
                        </tr>
                        <tr>
                            <td>188500</td>
                            <td>TOMATOES, GRAPE 12/1 PT</td>
                            <td class="rightjustifytext">10</td>
                        </tr>
                        <tr>
                            <td>122500</td>
                            <td>LETTUCE, ICEBERG LINER 24 CT</td>
                            <td class="rightjustifytext">10</td>
                        </tr>
                        <tr>
                            <td>121050</td>
                            <td>LETTUCE, GREEN LEAF 24 CT</td>
                            <td class="rightjustifytext">10</td>
                        </tr>
                    </table>
                </div>
            </div>

            <div class="col-md-6">
                <div class="topright">
                    <h2 class="sectiontext">Pricing Exceptions - Weekly Recap</h2>
                    <label class="redfont cccs">Red denotes Contract Item Overages</label>
                    </br>
                    <label class="cccs">For Weyand on the pricing week of - 7/31/2016</label>
                    <table>
                        <tr>
                            <th>PRO*ACT Member</th>
                            <th class="rightjustifytext">Total Occurrences of Summary Items</th>
                            <th class="rightjustifytext">Total Summary Exceptions</th>
                            <th class="rightjustifytext">Total Percentage of Summary Exceptions</th>
                        </tr>
                        <tr>
                            <td style="width:30%">Stern</td>
                            <td style="width:23%" class="rightjustifytext">205</td>
                            <td style="width:23%" class="rightjustifytext">2</td>
                            <td style="width:24%" class="rightjustifytext">99.02%</td>
                        </tr>
                        <tr>
                            <td>Hardies Dallas</td>
                            <td class="rightjustifytext">1,597</td>
                            <td class="rightjustifytext">0</td>
                            <td class="rightjustifytext">100.00%</td>
                        </tr>
                        <tr>
                            <td>Hardies South</td>
                            <td class="rightjustifytext">612</td>
                            <td class="rightjustifytext">1</td>
                            <td class="rightjustifytext">99.84%</td>
                        </tr>
                        <tr>
                            <td>Go Fresh</td>
                            <td class="rightjustifytext">482</td>
                            <td class="rightjustifytext">0</td>
                            <td class="rightjustifytext">100.00%</td>
                        </tr>
                        <tr>
                            <td>Segovias</td>
                            <td class="rightjustifytext">1,360</td>
                            <td class="rightjustifytext">2</td>
                            <td class="rightjustifytext">99.85%</td>
                        </tr>
                        <tr>
                            <td>Potato Spec</td>
                            <td class="rightjustifytext">1,605</td>
                            <td class="rightjustifytext">0</td>
                            <td class="rightjustifytext">100.00%</td>
                        </tr>
                        <tr>
                            <td class="rightjustifytext bold">TOTAL</td>
                            <td class="rightjustifytext bold">5,861</td>
                            <td class="rightjustifytext bold">5</td>
                            <td class="rightjustifytext bold">99.79%</td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-6">
                <div class="bottomleft">
                    <h2 class="sectiontext">Forecasted Spend - $9,814.81</h2>
                    <table>
                        <tr>
                            <th>Item Code</th>
                            <th class="rightjustifytext">Last Week's Usage</th>
                            <th class="rightjustifytext">This Week's Price</th>
                            <th class="rightjustifytext">Forecasted Spend</th>
                        </tr>

                        <tr>
                            <td>261650</td>
                            <td class="rightjustifytext">49</td>
                            <td class="rightjustifytext">3.14</td>
                            <td class="rightjustifytext">153.86</td>
                        </tr>
                        <tr>
                            <td>231083</td>
                            <td class="rightjustifytext">52</td>
                            <td class="rightjustifytext">1.25</td>
                            <td class="rightjustifytext">65.00</td>
                        </tr>
                        <tr>
                            <td>398980</td>
                            <td class="rightjustifytext">46</td>
                            <td class="rightjustifytext">4.95</td>
                            <td class="rightjustifytext">227.70</td>
                        </tr>
                        <tr>
                            <td>351135</td>
                            <td class="rightjustifytext">40</td>
                            <td class="rightjustifytext">0.75</td>
                            <td class="rightjustifytext">30.00</td>
                        </tr>
                        <tr>
                            <td>398036</td>
                            <td class="rightjustifytext">42</td>
                            <td class="rightjustifytext">3.00</td>
                            <td class="rightjustifytext">126.00</td>
                        </tr>
                        <tr>
                            <td>208110</td>
                            <td class="rightjustifytext">42</td>
                            <td class="rightjustifytext">2.50</td>
                            <td class="rightjustifytext">105.00</td>
                        </tr>
                        <tr>
                            <td>102800</td>
                            <td class="rightjustifytext">1835</td>
                            <td class="rightjustifytext">2.25</td>
                            <td class="rightjustifytext">4,128.75</td>
                        </tr>
                        <tr>
                            <td>367050</td>
                            <td class="rightjustifytext">1910</td>
                            <td class="rightjustifytext">1.95</td>
                            <td class="rightjustifytext">3,724.50</td>
                        </tr>
                        <tr>
                            <td>173100</td>
                            <td class="rightjustifytext">66</td>
                            <td class="rightjustifytext">19.00</td>
                            <td class="rightjustifytext">1,254.00</td>
                        </tr>
                        <tr>
                            <td class="bold">TOTAL</td>
                            <td class="bold rightjustifytext">4082</td>
                            <td class="bold rightjustifytext">--</td>
                            <td class="bold rightjustifytext">$9,814.81</td>
                        </tr>
                    </table>
                </div>
            </div>

            <div class="col-md-6">
                <div class="bottomright">
                    <h2 class="sectiontext">Delivery Performance</h2>
                    <table>
                        <tr>
                            <th>PRO*ACT Distributor</th>
                            <th>Restaurant Location</th>
                            <th class="rightjustifytext">Avg Order Amount</th>
                            <th class="rightjustifytext">Avg Package Count</th>
                            <th class="rightjustifytext">Total Sales</th>
                        </tr>

                        <tr>
                            <td>Sunrise FL</td>
                            <td>A1A ALEWORKS - #4405 - ST. AUGUSTINE</td>
                            <td class="rightjustifytext">$475.78</td>
                            <td class="rightjustifytext">28.50</td>
                            <td class="rightjustifytext">$1,903.10</td>
                        </tr>
                        <tr>
                            <td>Sunrise FL</td>
                            <td>RAGTIME TAVERN - #4404 - ATLANTIC BEACH</td>
                            <td class="rightjustifytext">$221.46</td>
                            <td class="rightjustifytext">17.50</td>
                            <td class="rightjustifytext">$885.82</td>
                        </tr>
                        <tr>
                            <td>Sunrise FL</td>
                            <td>SEVEN BRIDGES - #4403 - JACKSONVILLE</td>
                            <td class="rightjustifytext">$367.49</td>
                            <td class="rightjustifytext">22.67</td>
                            <td class="rightjustifytext">$1,102.47</td>
                        </tr>
                        <tr>
                            <td>T&T</td>
                            <td>BIG RIVER - #4201 - CHATTANOOGA</td>
                            <td class="rightjustifytext">$396.06</td>
                            <td class="rightjustifytext">22.83</td>
                            <td class="rightjustifytext">$2,376.34</td>
                        </tr>
                        <tr>
                            <td>T&T</td>
                            <td>BIG RIVER - #4205 - HAMILTON PL</td>
                            <td class="rightjustifytext">$424.74</td>
                            <td class="rightjustifytext">26.00</td>
                            <td class="rightjustifytext">$1,698.95</td>
                        </tr>
                        <tr>
                            <td class="bold">TOTAL</td>
                            <td></td>
                            <td class="bold rightjustifytext">3,770.42</td>
                            <td class="bold rightjustifytext">23.50</td>
                            <td class="bold rightjustifytext">$1,592.60</td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>

    </div>

</body>
</html> 

更新6

我知道Arturo的答案不仅仅是这个,但到目前为止只是添加了这个:

config.Routes.MapHttpRoute(
    name: "QuadrantData",
    routeTemplate: "api/{unit}/{begdate}/{enddate}"
);

...帮助了,因为我的方法中的断点正在达到。不幸的是,它突然发出“SyntaxError:UENpected token&lt; in JSON in position 0”

5 个答案:

答案 0 :(得分:3)

个人不是在javascript代码中使用@ Url.Action的忠实粉丝;这就是我调用ajax的方式:

  • 在布局页面中创建隐藏字段以存储根网址 - <input type="hidden" id="hdnRoot" value="@Url.Content("~/")" />
  • 有一个常量json文件来保存我的客户端常量(constants.js) - 将隐藏的字段值赋给serviceRoot变量:

    (function () {
    window.constants = {
    
        serviceRoot: $("#hdnRoot").val() + "api/",
        objectState: {
            added: "Added",
            modified: "Modified",
            unchanged: "Unchanged",
            deleted: "Deleted"
        },
       ..other values...
    };
    })();
    
  • 将您的ajax调用修改为:

    var root = window.constants.serviceRoot;
    $.ajax({
                type: 'GET',
                url: root + 'LandingPage/GetQuadrantData/'+unitval+'/'+begdateval+'/'+enddateval,
              //  data: { unit: unitval, begdate: begdateval, enddate: enddateval },
                contentType: 'application/json',
                cache: false,
                success: function (returneddata) {
                },
                error: function () {
                    alert('hey, boo-boo!');
                }
            });
    
     [RoutePrefix("api")]
     public class LandingPageController : ApiController {
    
       [HttpGet]
       [Route("GetQuadrantData/{unit}/{begdate}/{enddate}", Name="GetQuadrantDataFromLandingPage")]
       public HttpResponseMessage GetQuadrantData(string unit, string begdate, string enddate) 
    

答案 1 :(得分:2)

好的,此解决方案可以解决您的问题,但意味着您的代码会发生一些变化:

在视图中,当您调用Url.Action时,您尝试从mvc控制器生成webapi路由。你可以看到this question我说的如何做到这一点。

基本上,这个想法是使用Url.RouteUrl并使用额外的路由值httproute = true

现在,您需要更改代码中的某些部分才能使用它:

首先,您使用属性来定义您的web api路由,因此这些路由将在WebApiConfig类中定义的路由之后添加,并且在我引用的问题的答案中{{1将返回匹配路由值的第一个路由。因此,您需要在默认路由之前在Url.RouteUrl中声明路由:

WebApiConfig

如果您不这样做,将匹配默认路由,因为/* attributes removed */ public class LandingPageController : ApiController { public HttpResponseMessage GetQuadrantData(string unit, string begdate, string enddate) { ... public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.MapHttpAttributeRoutes(); /* Route added before the default one */ config.Routes.MapHttpRoute( name: "QuadrantData", routeTemplate: "api/{unit}/{begdate}/{enddate}" ); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } } 是可选的,{id}unitbegdate将作为查询参数传递。

修复此问题后,您还有另一个问题,即能够使用enddate您需要知道所有路径值(unit,begdate,enddate),这些值只有在执行脚本函数时才可用。但是,当为客户端生成 html 时,将在服务器中处理 .cshtml 中的所有剃刀代码。

正如您所看到的,当执行javascript时,您没有执行Url.RouteUrl'@Url.RouteUrl...',此时您只是在处理剃刀表达式处理时返回值的字符串。

您可以做的一件事是将“模板”值传递给'@Url.Action...'调用,并在使用实际值执行脚本时替换它:

Url.RouteUrl

var url = '@Url.RouteUrl(new { unit = "(unit)", begdate = "(begdate)", enddate = "(enddate)", httproute = true })' url = url.replace("(unit)", unitval) .replace("(begdate)", begdateval) .replace("(enddate)", enddateval) $.ajax({ type: 'GET', url: url, contentType: 'application/json', cache: false, success: function (returneddata) { }, error: function () { alert('hey, boo-boo!'); } }); 之后,变量replace的值将与此url类似。

如果您不想将路线移至/api/ABUELOS/2016-08-07/2016-08-13,可以在WebApiConfig中设置名称:

RouteAttribute

并在[RoutePrefix("api")] public class LandingPageController : ApiController { [Route("{unit}/{begdate}/{enddate}", Name = "QuadrantData")] public HttpResponseMessage GetQuadrantData(string unit, string begdate, string enddate) { ... 电话中指定路线名称:

Url.RouteUrl

这种方式对代码的干扰较少,但您需要知道要使用的路由的名称。

希望这有帮助。

答案 2 :(得分:1)

您在如何生成路线以及尝试访问路线方面存在一些问题。

您的Web API操作正在使用属性路由,因此默认情况下,没有像基于约定的路由中那样匹配的路由名称。

更新route属性以包含要在路由表中查找的名称。

[RoutePrefix("api")]
public class LandingPageController : ApiController {

    [HttpGet]
    [Route("{unit}/{begdate}/{enddate}", Name="QuadrantData")]
    public HttpResponseMessage GetQuadrantData(string unit, string begdate, string enddate) {
        _unit = unit;
        _beginDate = begdate;
        _endDate = enddate;
        //...other code
    }

    //...other code
}

接下来,即使您有名称,您还需要包含模板参数,以便从MVC获得匹配,并让它在您在操作上定义的模板中生成URL。

要生成指向Web API的链接,它将如下所示

@Url.RouteUrl(routeName : "QuadrantData", routeValues : new { httpRoute = true , unit = "ABUELOS", begdate = "2016-08-07", enddate = "2016-08-13"  })

@Url.HttpRouteUrl(routeName : "QuadrantData", routeValues : new { unit = "ABUELOS", begdate = "2016-08-07", enddate = "2016-08-13"  })

会将httpRoute添加到路线值。

参考:Construct url in view for Web Api with attribute routing

现在,如果您的方法不受限制,我建议采用以下替代方法。

KISS原则。将Web API(REST)端点更改为POST并更改其模板。

[RoutePrefix("api")]
public class LandingPageController : ApiController {

    //eg POST api/QuadrantData
    [HttpPost]
    [Route("QuadrantData", Name="GenerateQuadrantData")]
    public HttpResponseMessage QuadrantData(string unit, string begdate, string enddate) {
        _unit = unit;
        _beginDate = begdate;
        _endDate = enddate;
        //...other code
    }

    //...other code
}

并在JSON POST请求的正文中发送数据

$(function () {
    $("#btnGetData").click(function () {
        document.body.style.cursor = 'wait';
        var unitval = "ABUELOS"; //$('#unitName').val();
        var begdateval = $('#datepickerFrom').val();
        var enddateval = $('#datepickerTo').val();

        var jsonBody = JSON.stringify({ unit: unitval, begdate: begdateval, enddate: enddateval });

        $.ajax({
            type: 'POST',
            url: '@Url.HttpRouteUrl("GenerateQuadrantData", null)',
            contentType: 'application/json',
            dataType: 'json',
            data: jsonBody,
            cache: false,
            success: function (returneddata) {
                alert($(returneddata));
            },
            error: function () {
                alert('error in ajax');
            }
        });
    });

}); // end ready function

答案 3 :(得分:1)

关于你的观点:

  

如果我在浏览器中手动输入该URL,则URL栏会显示   “http://localhost:52194/api/ABUELOS/2016-08-21/2016-08-27”它有效 -   达到了这个方法并且它“做了它的事情。”

如下所示修改Ajax调用可以解决您的问题,并将GET请求发送到映射的URL(使用按钮单击事件处理程序显示):

 $("#btnGetData").click(function () {

   var unitval = 'ABUELOS';
   var begdateval = '2016-08-21';
   var enddateval = '2016-08-27';

   $.getJSON("api/" + unitval + "/" + begdateval + "/" + enddateval,
      function (Data) {
        // do what ever you want with the success response
      })
      .fail(
        function (jqXHR, textStatus, err) {
          // do what ever you want with the failed response
      });
 });

答案 4 :(得分:-1)

当您需要完整网址来完成请求时,您只需向jQuery提供控制器和操作名称。此外,您的终点有一个&#39; api&#39;字首。如果您的服务器在本地运行,您需要将url属性设置为http://localhost:{port} / api / {controller} / {action}。