javascript - 另一个函数运行成功javascript后无法赋值
问题描述
我有一些这样的代码
我有加载功能,它调用testRequest
功能。
testRequest
函数调用ajax服务并返回响应。
但是testRequest
函数运行成功后,加载函数得到未定义的值
angular.module('MyApp', []).controller("AppCtrl", ['AjaxService', function(ajax) {
var l_this = this;
this.testRequest = function() {
var req = ajax.getstatus('https://testapi.io/api/nguyenthemanh2601/testangular');
req.then(function(rep) {
var arr = [];
if (rep.status == 400) {
for (i = 1; i < 10; i++) {
arr.push(rep.data + i);
}
return arr;
} else {
for (i = 1; i < 10; i++) {
arr.push(rep.data + i);
}
return arr;
}
});
};
this.load = function() {
var res = l_this.testRequest();
console.log(res);
l_this.status = res;
}
}]);
请帮忙!
解决方案
angular.module('MyApp', []).controller("AppCtrl", ['AjaxService', function(ajax) {
var l_this = this;
this.testRequest = function() {
$http({
method: 'POST',
url: 'https://testapi.io/api/nguyenthemanh2601/testangular',
}).
success(function(rep) {
if (rep.status == 200) {
for (i = 1; i < 10; i++) {
arr.push(rep.data + i);
}
return arr;
}
}).
error(function(rep) {
if (rep.status == 400) {
for (i = 1; i < 10; i++) {
arr.push(rep.data + i);
}
return arr;
}
});
};
// Next Execution.....
}]);
Note You can handle success and error message based on that response execute the next step(handle error and success).
更多信息... $http请求角度
推荐阅读
- c# - 检查 HttpClient 是否有连接
- antd - 带有开关组件的 antd 表
- javascript - Sublime text 3 格式/将单行转换为多行
- php - 在文本 PHP Wordpress 中查找特定链接
- amazon-web-services - AWS lambda 顺序执行
- html - 获取ts文件中的视频元素
- jenkins-plugins - 如何在 Jenkins Bulk Builder 插件中使用参数?
- javascript - 无法让 SSL/TLS 和节点正常运行并连接到 SMTP
- django - 存储日志的 Django Gunicorn
- python - 计算列表的所有组合的乘积