Javascript 2$http get函数
给定2个JSON url,如何确保代码已完成从a.JSON检索数据,然后仅开始从b.JSON检索数据,然后仅运行init函数Javascript 2$http get函数,javascript,angularjs,http,get,Javascript,Angularjs,Http,Get,给定2个JSON url,如何确保代码已完成从a.JSON检索数据,然后仅开始从b.JSON检索数据,然后仅运行init函数 var aUrl = "a.json"; var bUrl = "b.json"; 我的尝试: var app = angular.module('calendarApp', []); app.controller('ctrl', function($scope, $http) { $http.get(aUrl).success(function(data) {
var aUrl = "a.json";
var bUrl = "b.json";
我的尝试:
var app = angular.module('calendarApp', []);
app.controller('ctrl', function($scope, $http) {
$http.get(aUrl).success(function(data) { });
$http.get(bUrl).success(function(data) {
init()}
);
var init = function(){}
可能不是最好或最干净的方法,但可以快速使您的代码执行您希望它执行的操作我得到:
var app = angular.module('calendarApp', []);
app.controller('ctrl', function($scope, $http) {
$http.get(aUrl).success(function(data) {
$http.get(bUrl).success(function(data) {
init()
}
});
);
var init = function(){}
可能不是最好或最干净的方法,但可以快速使您的代码执行您希望它执行的操作我得到:
var app = angular.module('calendarApp', []);
app.controller('ctrl', function($scope, $http) {
$http.get(aUrl).success(function(data) {
$http.get(bUrl).success(function(data) {
init()
}
});
);
var init = function(){}
您可以创建一个服务层,在其中定义这两种方法。然后将服务注入控制器:
//Controller
YourService.getUrl(urlA).then(function(response) {
if(response != null && response.success == true){
// do something
}
YourService.getUrl(urlB).then(function(response) {
if(response != null && response.success == true){
// do something
init()
}
},
function errorCallback(response) {
console.log("Error YourService: getUrlB ---> ");
});
},
function errorCallback(response) {
console.log("Error YourService: getUrlA ---> ");
});
// Example of method in your service
this.getUrl = function(urlA) {
try{
var deferred = $q.defer();
$http({
method: 'GET',
url: getUrlA,
params: {},
responseType: "json",
cache: false
})
.success(function(data, status, headers, config) {
deferred.resolve(data);
})
.error(function(data, status, headers, config) {
deferred.reject(data);
});
return deferred.promise;
}catch(e){
/* */
console.log("Service: getUrl ---> " + e);
}
}
您可以创建一个服务层,在其中定义这两种方法。然后将服务注入控制器:
//Controller
YourService.getUrl(urlA).then(function(response) {
if(response != null && response.success == true){
// do something
}
YourService.getUrl(urlB).then(function(response) {
if(response != null && response.success == true){
// do something
init()
}
},
function errorCallback(response) {
console.log("Error YourService: getUrlB ---> ");
});
},
function errorCallback(response) {
console.log("Error YourService: getUrlA ---> ");
});
// Example of method in your service
this.getUrl = function(urlA) {
try{
var deferred = $q.defer();
$http({
method: 'GET',
url: getUrlA,
params: {},
responseType: "json",
cache: false
})
.success(function(data, status, headers, config) {
deferred.resolve(data);
})
.error(function(data, status, headers, config) {
deferred.reject(data);
});
return deferred.promise;
}catch(e){
/* */
console.log("Service: getUrl ---> " + e);
}
}
$http.get返回一个承诺,因此您可以执行以下操作:
return $http.get(aUrl)
.then(function(result) {
return $http.get(bUrl);
})
.then(function(result) {
return init();
},
function (error) {
// do something with the error
});
$http.get返回一个承诺,因此您可以执行以下操作:
return $http.get(aUrl)
.then(function(result) {
return $http.get(bUrl);
})
.then(function(result) {
return init();
},
function (error) {
// do something with the error
});
我建议使用AngularJS承诺。它的主要优点是可以同时异步加载数据,而无需等待第一个请求完成。见:$q
var承诺=[];
var loadingJson=函数(url){
var defer=$q.defer();
$http.get(url).then(函数(结果){
推迟、解决(结果);
},函数(err){
延迟、拒绝(错误);
});
回报、承诺;
};
promise.push(加载json('example.com/1.json');
promise.push(加载json('example.com/2.json');
$q.all(承诺)。然后(函数(结果列表){
//在这里,resultList包含两个API调用的结果。
},函数(errList){
//你的错误处理在这里。
});
我建议使用AngularJS承诺。它的主要优点是可以同时异步加载数据,而无需等待第一个请求完成。见:$q
var承诺=[];
var loadingJson=函数(url){
var defer=$q.defer();
$http.get(url).then(函数(结果){
推迟、解决(结果);
},函数(err){
延迟、拒绝(错误);
});
回报、承诺;
};
promise.push(加载json('example.com/1.json');
promise.push(加载json('example.com/2.json');
$q.all(承诺)。然后(函数(结果列表){
//在这里,resultList包含两个API调用的结果。
},函数(errList){
//你的错误处理在这里。
});
1。JavaScript是同步的
Synchronous Example[Flow in sequence]:
console.log('1')
console.log('2')
console.log('3')
$http.get(aUrl)
.then(function(response){
console.log('inside the first then response');
console.log(response.data);
//executing the second request after we get the first request
//and returns the **outputResponse** which is captured in the next **then** block
return $http.get(bUrl);
})
.then(function(**outputResponse** ){
console.log('outputResponse generated from to the second bUrl');
//you can call init() here
});
它记录了1 2 3
拨打服务电话的示例
1. $http.get(aUrl).success(function(data) { console.log('1.any time response returns') });
2. $http.get(bUrl).success(function(data) { console.log('2.mummy returns')};
因此,单线程javascript将首先使用$http.get(aUrl)调用下面的代码,它点击url并处理从后台获取数据的过程
Synchronous Example[Flow in sequence]:
console.log('1')
console.log('2')
console.log('3')
$http.get(aUrl)
.then(function(response){
console.log('inside the first then response');
console.log(response.data);
//executing the second request after we get the first request
//and returns the **outputResponse** which is captured in the next **then** block
return $http.get(bUrl);
})
.then(function(**outputResponse** ){
console.log('outputResponse generated from to the second bUrl');
//you can call init() here
});
以上代码满足您的要求
1.JavaScript是同步的
Synchronous Example[Flow in sequence]:
console.log('1')
console.log('2')
console.log('3')
$http.get(aUrl)
.then(function(response){
console.log('inside the first then response');
console.log(response.data);
//executing the second request after we get the first request
//and returns the **outputResponse** which is captured in the next **then** block
return $http.get(bUrl);
})
.then(function(**outputResponse** ){
console.log('outputResponse generated from to the second bUrl');
//you can call init() here
});
它记录了1 2 3
拨打服务电话的示例
1. $http.get(aUrl).success(function(data) { console.log('1.any time response returns') });
2. $http.get(bUrl).success(function(data) { console.log('2.mummy returns')};
因此,单线程javascript将首先使用$http.get(aUrl)调用下面的代码,它点击url并处理从后台获取数据的过程
Synchronous Example[Flow in sequence]:
console.log('1')
console.log('2')
console.log('3')
$http.get(aUrl)
.then(function(response){
console.log('inside the first then response');
console.log(response.data);
//executing the second request after we get the first request
//and returns the **outputResponse** which is captured in the next **then** block
return $http.get(bUrl);
})
.then(function(**outputResponse** ){
console.log('outputResponse generated from to the second bUrl');
//you can call init() here
});
以上代码满足您的要求
带承诺,这代表回调。你通过将一个放在另一个里面将它们链接在一起,带承诺,这代表回调。你通过将一个放在另一个里面将它们链接在一起。这可以在你的控制器中,也可以放在单独的服务中。如果需要,这将使内容更好地可测试。对于resultList,resultList[0]是否总是从1.json和resultList[1]返回结果总是从2.json返回结果?我如何确定哪一个是从1.json得到的结果,哪一个是从2.json得到的结果?这可以放在控制器中,也可以放在单独的服务中。如果需要,这将使内容更易于测试。对于resultList,resultList[0]是否总是从1.json和resultList[1]返回结果总是从2.json返回结果?我如何确定哪个结果来自1.json,哪个结果来自2.json?