我有一个使用Web API后端的angularJS站点。我试图做一个保存和我期待着回到其下方AngularJS save()返回来自Web API的错误响应
public enum ResultType
{
Success,
InvalidAccountNumber,
InvalidAddress,
NotFound,
Incomplete,
Error,
Unauthorized
}
这里定义的与resultType枚举是我用来设置我的资源的代码。
app.factory('NotificationOptInApi', function ($resource) {
return $resource(API_SOURCE + 'NotificationOptIn', {}, {
'save': { method: 'POST', isArray: true }
});
});
这里是我的电话,以节省
$scope.saveNotifications = function() {
var result = NotificationOptInApi.save($scope.notifications, function() {
// check for failure
if (result !== 'Success') {
$scope.saveError = result;
return;
}
});
};
这里是我所得到的返回
{0: "S", 1: "u", 2: "c", 3: "c", 4: "e", 5: "s", 6: "s", $get: function, $save: function, $query: function, $remove: function, $delete: function}
这里是返回它的代码。 Thsi是当我试图返回一个列表,而不是一个reusltType
public List<ResultType> Post([FromBody]dynamic notificationSubscriptionHolder)
{
List<NotificationSubscriptionData> notificationSubscriptions = notificationSubscriptionHolder.ToObject<List<NotificationSubscriptionData>>();
Logger.Info("Update Account Notifications", string.Format("<updateAccountNotificationsToUser><NotificationSubscriptionData>{0}</NotificationSubscriptionData><user>{1}</user></updateAccountNotificationsToUser>", notificationSubscriptions, this.CurrentIdentity.Name));
try
{
return _accountManager.UpdateAccountNotifications(this.CurrentIdentity.Name, notificationSubscriptions);
}
catch(Exception ex)
{
return new List<ResultType>() { ResultType.Error };
}
}
这在其他地方没有问题工作的。我可以检查结果,它会是'成功'。我在这里看到的差异是我保存一个数组,所以我不得不将IsArray设置为true。我目前在我正在保存的数组中有1项。我得到的回应是一组字符。放在一起他们拼出“成功”。
有没有办法让我不把我的回应作为一个数组,即使我保存数组?我也尝试返回一个列表,思考我需要返回一个数组。当我这样做时,我得到一个有1个物品的数组,但是在这1个物品中,我有一组表示成功的人物。
可以显示你的角码 –
非常奇怪的角度文档中的IsArray应该说,返回的对象是否是一个数组,我只返回一个ResultType,但如果我有IsArrya设置为false,我得到一个推送错误。如果我将它设置为true,我可以成功保存但返回数组。 –