加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何使用AngularJS在我的http请求中发送参数?

发布时间:2020-12-17 07:41:18 所属栏目:安全 来源:网络整理
导读:我使用以下代码: $http({ method: 'GET',url: '/Admin/GetTestAccounts',data: { applicationId: 3 }}).success(function (result) { $scope.testAccounts = result;}); 代码将以下内容发送到我的服务器: http://127.0.0.1:81/Admin/GetTestAccounts 当我
我使用以下代码:
$http({
    method: 'GET',url: '/Admin/GetTestAccounts',data: { applicationId: 3 }
}).success(function (result) {
    $scope.testAccounts = result;
});

代码将以下内容发送到我的服务器:

http://127.0.0.1:81/Admin/GetTestAccounts

当我的MVC控制器收到此信息时:

[HttpGet]
public virtual ActionResult GetTestAccounts(int applicationId)
{
    var testAccounts =
        (
            from testAccount in this._testAccountService.GetTestAccounts(applicationId)
            select new
            {
                Id = testAccount.TestAccountId,Name = testAccount.Name
            }
        ).ToList();

    return Json(testAccounts,JsonRequestBehavior.AllowGet);
}

它抱怨没有applicationId.

The parameters dictionary contains a null entry for parameter ‘applicationId’ of non-nullable type ‘System.Int32’ for method

有人可以解释为什么applicationId不作为参数发送?以前我正在使用以下非角度代码,它工作得很好:

$.ajax({
    url: '/Admin/GetTestAccounts',data: { applicationId: 3 },type: 'GET',success: function (data) {
        eViewModel.testAccounts(data);
    }
});
如果你不想使用jQuery的$.param,你可以使用序列化对象的$http的param域.
var params = {
    applicationId: 3
}

$http({
    url: '/Admin/GetTestAccounts',method: 'GET',params: params
});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读