【问题标题】:How to pass id to get data from api如何通过 id 从 api 获取数据
【发布时间】:2015-01-30 14:01:04
【问题描述】:

我有 api 控制器 public string GetCountries(int id){},我也有 ajax 脚本

<script>
        $(document).ready(function () {
            var id = $(this).attr('href');
            $("#xid").click(function () {
              $.ajax({

                    type: "GET",
                    dataType: "json",
                    url: "/api/Sports/GetCountries/" +  id,
                    success: function (data) {
                        obj = JSON.parse(data);
                        alert(data);

                            for (var i = 0; i < data.length; i++) {

                                $("#getCountry").append("<a href=" + obj[i].Id + ">" + obj[i].Name + "</>" + "<br/>");
                            }

                    }
                });
            });

        });
    </script>

我的观点是这样的

<div style="float:left">
    <div ng-app="" ng-controller="biloKojiKontroler">
        <table class="tg">
            <tr ng-repeat="x in biloKojeIme">
                <th>
                <a href="#" id="xid" value="{{x.id}}">{{x.name}}</a>


               </th>

            </tr>
        </table>
    </div>
</div>

我想单击锚标记以从我的 api 控制器获取数据,但是当我单击它时将我重定向到另一个页面。

【问题讨论】:

  • 检查页面加载后是否有javascript错误,这是做什么的$(this).attr('href')
  • 你可以问你的朋友。,,, stackoverflow.com/questions/28235639/…的副本
  • 没有错误...只是重定向到另一个页面
  • 我试过了,但没有任何反应
  • 同样...没有用

标签: jquery asp.net-mvc angularjs


【解决方案1】:

您在错误的位置获取链接的 href,它应该在点击处理程序中。
您已将 dataType 设置为 json,因此您的成功回调中的 data 参数将已被解析。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-04
    • 2021-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多