I am using the following code:
我使用的代码如下:
$http({
method: 'GET',
url: '/Admin/GetTestAccounts',
data: { applicationId: 3 }
}).success(function (result) {
$scope.testAccounts = result;
});
The code sends the following to my server:
代码将以下内容发送到我的服务器:
http://127.0.0.1:81/Admin/GetTestAccounts
When this is received by my MVC controller:
当我的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);
}
It complains that there is no applicationId.
它抱怨没有applicationId。
The parameters dictionary contains a null entry for parameter 'applicationId' of non-nullable type 'System.Int32' for method
parameters dictionary包含非空类型'系统的参数“applicationId”的空条目。Int32”方法
Can someone explain why the applicationId is not being sent as a parameter? Previously I was doing this with the following non-Angular code and it worked just fine:
有人能解释为什么不将applicationId作为参数发送吗?之前我用下面的非角码来做这个,效果很好:
$.ajax({
url: '/Admin/GetTestAccounts',
data: { applicationId: 3 },
type: 'GET',
success: function (data) {
eViewModel.testAccounts(data);
}
});
2 个解决方案
#1
31
If you don't want to use jQuery's $.param you can use $http's param field which serializes an object.
如果您不想使用jQuery的$。您可以使用$http的param字段来序列化对象。
var params = {
applicationId: 3
}
$http({
url: '/Admin/GetTestAccounts',
method: 'GET',
params: params
});
#2
3
Ok, I will try to answer this.
好的,我试着回答这个问题。
I think the problem is that angularjs presume that data passed to http will be urlencoded. I am not sure why angular doesn't serialize it implicitly if there's an object. So you have to encode it yourself:
我认为问题在于angularjs认为传递到http的数据将是urlencodes。我不确定如果有一个物体角为什么不隐式地序列化它。所以你必须自己编码
$http({
method: 'GET',
url: '/Admin/GetTestAccounts',
data: 'applicationId=3'
})
or use jQuery param to encode it for you:
或使用jQuery param为您编码:
$http({
method: 'GET',
url: '/Admin/GetTestAccounts',
data: $.param({ applicationId: 3 })
})
#1
31
If you don't want to use jQuery's $.param you can use $http's param field which serializes an object.
如果您不想使用jQuery的$。您可以使用$http的param字段来序列化对象。
var params = {
applicationId: 3
}
$http({
url: '/Admin/GetTestAccounts',
method: 'GET',
params: params
});
#2
3
Ok, I will try to answer this.
好的,我试着回答这个问题。
I think the problem is that angularjs presume that data passed to http will be urlencoded. I am not sure why angular doesn't serialize it implicitly if there's an object. So you have to encode it yourself:
我认为问题在于angularjs认为传递到http的数据将是urlencodes。我不确定如果有一个物体角为什么不隐式地序列化它。所以你必须自己编码
$http({
method: 'GET',
url: '/Admin/GetTestAccounts',
data: 'applicationId=3'
})
or use jQuery param to encode it for you:
或使用jQuery param为您编码:
$http({
method: 'GET',
url: '/Admin/GetTestAccounts',
data: $.param({ applicationId: 3 })
})