我有一个混合的ASP.NET MVC视图页面,它有一个角度控制器,它在工厂中使用$ resource的save()和delete()方法作为下面的代码剪辑。控制器的delete()方法有效。执行add()时,Fiddler显示状态201(已创建),新用户被添加到数据库,但IE F12控制台显示以下[$ resource:badcfg]错误。成功或失败回调都不会执行。没有警报也没有屏幕刷新。为了您的参考,我添加了Angular $资源工作的WebAPI的post()动作。我还修改了$ resource action参数以将isArray设置为false。我仍然得到相同的[$ resource:badcfg]错误。返回结果中是否存在Angular不期望的内容?谢谢你的帮助。
[$resource:badcfg] http://errors.angularjs.org/1.4.6/$resource/badcfg?p0=save&p1=object&p2=array&p3=POST&p4=http%3A%2F%2FmyIISServerName%3A8083%2FWCF_WebAPI%2FCMT%2Fapi%2FuserFSR%2Fadd%2F129%2F1540
ufsrApp.factory('userFsrAddFactory', ['$resource',
function($resource) {
return $resource(userFsrURI + 'userFSR/add/:userId/:fsrId', {
userId: '@userId',
fsrId: '@fsrId'
},
{
save: {method: 'POST', isArray: false}
});
}
])
ufsrApp.factory('userFsrDeleteFactory', ['$resource',
function($resource) {
return $resource(userFsrURI + 'userFSR/delete/:userId/:ufsrId'
//,
// { userId: '@userId', ufsrId: '@ufsrId' }
);
}
])
ufsrApp.controller('ufsrController', [...omitted for brevity... ,
function(....omitted for brevity....) {
$scope.add = function() {
$scope.newUserFSR = new userFsrAddFactory();
$scope.newUserFSR.userId = $scope.userId;
$scope.newUserFSR.fsrId = $scope.role;
userFsrAddFactory.save($scope.newUserFSR,
function() { //success callback
alert("New role added");
$scope.postStatus = true;
//using jquery location object to reload the page after adding a new user
location.reload(true);
},
function() { //failure callback
alert("add role failed");
$scope.postStatus = false;
}
);
}
}]);
以下是Angular控制器发布到的WebAPI的Post()。
/// <summary>
/// create new user FSR
/// </summary>
/// <param name="userId"></param>
/// <param name="fsrId"></param>
/// <returns></returns>
[Route("add/{userId:int}/{fsrId:int}")]
public async Task < HttpResponseMessage > Post(int userId, int fsrId) {
try {
var userFSR = await repo.GetUserFSR(userId);
if (userFSR == null)
return Request.CreateResponse(HttpStatusCode.NotFound);
else {
int newKey = await repo.PostUserFSR(userId, fsrId);
return Request.CreateResponse(HttpStatusCode.Created, userFSR);;
}
} catch (Exception ex) {
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
}
}
答案 0 :(得分:1)
save()
如果那是你想要做的,你应该让你的代码真正做到这一点。现在你在调用perf stat -B -e cache-references,cache-misses,cycles,instructions,branches,faults,migrations sleep 5
Performance counter stats for 'sleep 5':
之后,但在对服务器的请求收到响应之前,甚至在请求完全发送到服务器之前运行了(导致你看到的错误)。
这应该是你的成功回调。或者根本不重新加载 - 你应该能够轻松地使事情无需重新加载。
答案 1 :(得分:0)
我终于找到了问题所在。它转向我的WebAPI post方法除了Create(201)状态之外还返回userFSR对象。所以我将上面的代码片段修改为:
返回Request.CreateResponse(HttpStatusCode.Created);