Javascript 嵌套承诺:进行两次背对背的承诺调用,传递值
我目前正在为我的电子应用程序建立一个许可证系统。但我在以下方面没有取得成功:Javascript 嵌套承诺:进行两次背对背的承诺调用,传递值,javascript,ajax,Javascript,Ajax,我目前正在为我的电子应用程序建立一个许可证系统。但我在以下方面没有取得成功: 承诺提示用户输入(电子提示模块) 之后立即对服务器进行ajax调用 评估响应(来自服务器端脚本的1\n或0\n) 令人头痛的是,在从prompt(其结构为promise)获取用户输入后运行ajax调用,并实际等待它完成 到目前为止,我一直在努力调整我的承诺,例如: (一) (二) 这就是我目前所处的位置,ajax函数不会返回任何要传递的内容 第一个是提示符,它相应地工作,返回其值 function activeVali
function activeValidation(){
try{
prompt({
title: "Enter key",
label: "Enter your key",
value: "",
alwaysOnTop: true,
autoHideMenuBar: true,
inputAttrs: {
type: 'text'
},
type: "input"
})
令人头痛的是在这个领域,ajax领域
.then(function(userinput){
return new Promise(function(resolve,userinput){
var req = new XMLHttpRequest();
req.onreadystatechange = function(){
if(req.readyState == 4 && req.status == 200){
resolve(req.responseText);
}
}
req.open("GET","someCGIscript?key="+userinput,true);
req.send();
}).then(function(response){
if(response == "1\n"){
runProgram();
}
else{
}
}).catch(function(e){
console.error(e);
terminate();
})});
}
catch(e){
console.log(e.name);
console.log(e.message);
terminate();
}
}
我已经尝试从resolve(req.responseText)打印ajax承诺的值代码>但在以下段中返回未定义
在那之后,我试图重新构建这两个承诺(嵌套的和作为第一个承诺的)。没有运气
我相信这是一个正确构建承诺的问题,但我仍然是承诺的新手,至少在js:)中是如此。希望这对你有帮助。提示符将自行处理用户输入。因此,当您收到输入时,执行ajax调用并获取服务器响应。这是你希望的,就我理解你的问题而言
const { app, BrowserWindow } = require('electron')
const path = require('path')
const prompt = require('electron-prompt'); // assume that this is what you use
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
let mainWindow
function createWindow() {
prompt({
title: 'Prompt example',
label: 'License Key:',
value: 'xxxx-xxxx-xxxx-xxxx',
inputAttrs: {
type: 'Key'
},
type: 'input'
})
.then((r) => {
if (r === null) {
console.log('user cancelled');
} else {
var data = {key: r}; // accepting the user input
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
console.log(this.responseText)
console.log(JSON.parse( this.responseText).status===1?'License complete':'Re enter a valid key')// key validation with the server response
}
});
xhr.open("POST", "https://76c6d974-0305-4a40-b499-6b3efee4938f.mock.pstmn.io/license");
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("Cache-Control", "no-cache");
xhr.send(JSON.stringify( data));
}
})
.catch(console.error);
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
mainWindow.loadFile('index.html')
mainWindow.on('closed', function () {
mainWindow = null
})
}
app.on('ready', createWindow)
祝你好运!如果这有助于让其他人更容易找到,请投票。您的新承诺
回调函数中存在参数不匹配。第二个参数与userinput
无关,而是提供给您的拒绝回调(以防您需要它)。通过调用userinput
您将无法访问userinput
的实际值,因为它被传递到其上方一行
因此,HTTP请求将被发送到类似于someCGIscript?key=function(){[native code]}
,这显然会导致不希望的HTTP响应
因此,在newpromise
回调函数参数列表中,要么调用它reject
,要么完全忽略它
.then(function(userinput){
return new Promise(function(resolve,userinput){
var req = new XMLHttpRequest();
req.onreadystatechange = function(){
if(req.readyState == 4 && req.status == 200){
resolve(req.responseText);
}
}
req.open("GET","someCGIscript?key="+userinput,true);
req.send();
}).then(function(response){
if(response == "1\n"){
runProgram();
}
else{
}
}).catch(function(e){
console.error(e);
terminate();
})});
}
catch(e){
console.log(e.name);
console.log(e.message);
terminate();
}
}
const { app, BrowserWindow } = require('electron')
const path = require('path')
const prompt = require('electron-prompt'); // assume that this is what you use
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
let mainWindow
function createWindow() {
prompt({
title: 'Prompt example',
label: 'License Key:',
value: 'xxxx-xxxx-xxxx-xxxx',
inputAttrs: {
type: 'Key'
},
type: 'input'
})
.then((r) => {
if (r === null) {
console.log('user cancelled');
} else {
var data = {key: r}; // accepting the user input
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
console.log(this.responseText)
console.log(JSON.parse( this.responseText).status===1?'License complete':'Re enter a valid key')// key validation with the server response
}
});
xhr.open("POST", "https://76c6d974-0305-4a40-b499-6b3efee4938f.mock.pstmn.io/license");
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("Cache-Control", "no-cache");
xhr.send(JSON.stringify( data));
}
})
.catch(console.error);
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
mainWindow.loadFile('index.html')
mainWindow.on('closed', function () {
mainWindow = null
})
}
app.on('ready', createWindow)