【发布时间】:2015-05-10 11:59:48
【问题描述】:
我正在调用多个 ajax 调用,但代码仅在执行所有 ajax 调用后才到达 API。
Javascript:
function test = function(){
var entity = {};
entity.Number = 1;
appFactory.testPostCall(entity, 'ApiController/TestMethod');
entity.Number = 2;
appFactory.testPostCall(entity, 'ApiController/TestMethod');
}
应用工厂
factory.testPostCall = function (number, appendUrl) {
var q = $q.defer();
$http({
method: "POST",
url: url + appendUrl,
data: number
}).success(function (data, status, headers, config) {
q.resolve(data);
}).error(function (data, status, headers, config) {
q.reject(data);
});
return q.promise;
}
API
[HttpPost]
public Nullable<int> TestMethod(TestEntity entity)
{
return entity.Number;
}
我跟踪了如何通过断点运行代码。调用 test() 函数执行以下操作:
javascript -> appFactory
javascript -> appFactory
API
API
//with the parameter Entity having the value Entity.Number = 2 for both API calls.
我尝试在
处放置断点entity.Number = 2;
并等待 API 被调用,但似乎代码正在等待函数结束,直到 API 被调用。我对这种行为感到非常困惑,我实际上期待类似以下内容:
javascript -> appFactory -> API //entity.Number = 1
javascript -> appFactory -> API //entity.Number = 2
链接运作良好,但我需要独立运行,我真的很想了解发生了什么。
entity.Number = 1;
appFactory.testPostCall(entity, 'ApiController/TestMethod')
.then(function(data){
entity.Number = 2;
appFactory.testPostCall(entity, 'ApiController/TestMethod');
});
谢谢!!!
【问题讨论】:
标签: javascript ajax angularjs post asynchronous