我需要从上述操作中接收值,该操作使用HTML表单从用户处收集数据,并将其传递给外部JavaScript文件,该文件在处理完这些值后会返回一个字符串。该JavaScript文件已与Babel一起作为ES6的捆绑包进行了翻译,该捆绑包使用Promises处理值并返回字符串结果。现在,我使用.then
来使AngularJS等待响应,然后再进行$http
调用。结果返回,但不用作传递给我的.then
运算符的值。拜托,我该如何解决?以下是我的代码段:
var numBits = $scope.getCertKeySize();
try{
CreateRequest($scope.fields['env'], numBits).then(function(result){
$scope.fields['key'] = result; //result comes back undefined
var jsonMsg = $scope.fields;
$http({
method: 'POST',
url: '/requestedStuff',
data: jsonMsg,
headers: {'Content-type': 'application/json'}
}).then(function successCallback(response){
$scope.disableSubmit = true;
});
});
}catch(err){
return false;
}
function CreateRequest($scope.fields['env'], numBits){
var fromCSForm = document.fromLiveApp;
var userId = fromCSForm.txtUserId.value;
var department = fromCSForm.txtDepartment.value;
return createFromExternalJSFile(userId, department);//returns a promise with the result
}
这是来自createFromExternalJSFile的示例代码:
function createFromExternalJSFile(userId, department) {
return Promise.resolve().then(function () {
return createPKCS10Internal(userId, department);
}).then(function () {
var resultString = "-----BEGIN CERTIFICATE REQUEST-----\r\n";
resultString = "".concat(resultString).concat(formatPEM((0, _pvutils.toBase64)((0, _pvutils.arrayBufferToString)(pkcs10Buffer))));
resultString = "".concat(resultString, "\r\n-----END CERTIFICATE REQUEST-----\r\n");
});
}
请原谅我的无知,因为我对AngularJS和JavaScript的了解不尽如人意。谢谢!
答案 0 :(得分:1)
您应该从上一个.then函数返回值,例如:
function createFromExternalJSFile(userId, department) {
return Promise.resolve().then(function () {
return createPKCS10Internal(userId, department);
}).then(function () {
var resultString = "-----BEGIN CERTIFICATE REQUEST-----\r\n";
resultString = "".concat(resultString).concat(formatPEM((0, _pvutils.toBase64)((0, _pvutils.arrayBufferToString)(pkcs10Buffer))));
resultString = "".concat(resultString, "\r\n-----END CERTIFICATE REQUEST-----\r\n");
return resultString;
});
}