【发布时间】:2018-10-19 13:01:31
【问题描述】:
我正在尝试等待服务器返回一些数据,然后再进行一次回调。这是我的代码
commonApp.factory('replyMessageFactory', ['$q', '$http', '$log', function ($q, $http, $log) {
var factoryObj = {};
factoryObj.send = function (obj) {
var req = {
method: 'POST',
url: "/api/ThreadMessage",
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
data: obj
}
return $http(req);
}
return factoryObj;
}
]);
我如下调用这个工厂方法
scope.sendReply = function () {
updateModel();
var replyModelValue = scope.replyModel;
replyMessageFactory.send(scope.replyModel).then(function (data) {
scope.clear();
scope.updateThread(replyModelValue);
});
}
问题是我没有从后端获取最新数据。如果我添加超时,那么它可以工作。会不会也是后端的原因?
我的后端api是这个
public async Task<HttpResponseMessage> Post(ThreadMessageModel messageData)
{
try
{
await _findMessageService.AddMessageReply(findmessage);
return new HttpResponseMessage(HttpStatusCode.OK);
}
catch (Exception exception)
{
Log.Log(EventSeverity.Error, new { Message = "Could not create message", exception });
throw new Exception(string.Format("Kan ikke opprette melding. {0}", exception));
}
}
public Task<int> AddMessageReply(FindMessage message)
{
try
{
client.Update<FindMessage>(message.Id).Field(x => x.Replies, message.Replies).Execute();
return Task.FromResult(1);
}
catch (Exception exception)
{
log.Log(EventSeverity.Error, "Could not add message and feedback status", exception);
throw;
}
}
有什么想法吗?
【问题讨论】:
-
$http返回一个承诺,所以using$qis not needed,这是一个反模式。你可以试试:return $http.post(url,obj).then((res) => {return res.data;}) -
这样也行不通
-
请告诉我们您是如何呼叫
send以及updateThread是做什么的。您发布的第二个 sn-p 看起来不错。 -
更新了问题
-
您最多有 4 张重新打开投票,所以再多一张,它将重新打开。我怀疑我们有一个金票持有者在其中一个被骗者身上发现了类似的答案,但这不是标记为重复问题的正当理由。
标签: javascript c# angularjs angular-promise