0
セットアップしたエンドポイントにajaxコールを正常に作成しました。 JSONとして未処理の証明書を送り、バックエンドがそれをデコードします。今、私はconsole.log
でデコードされた結果を呼び出すことができますが、結果として返す方法を理解することはできません。NodeJSエンドポイントからjQuery AJAXの結果を取得する方法は?
AJAX呼び出し:
$("#decoderSubmit").on('click', function() {
var body = $('#csr').val();
if (!(body.match(new RegExp("-----BEGIN", 'i')))) {
$('#csrFail').show();
} else {
if (body.match(new RegExp("REQUEST", 'i'))) {
$("#csrAlert").show();
} else
$("#certAlert").show();
decode(body);
}
});
function decode(body) {
$.ajax({
type: 'POST',
url: '/api/decoder',
data: {
body: body //cert/csr
},
dataType: 'JSON',
timeout: 4500,
success: callback,
error: fail
});
function callback(data) {
//decodedText = data;
alert(data);
//$("#decodeBody").append('<div>' + data + '</div>');
}
function fail(request, status, err) {
console.log(err);
}
}
NodeJSエンドポイント:
router.route('/decoder')
//console.log('test');
.post(function(req, res) {
let body = req.body.body;
decode(body, function(result) {
res.json(result);
});
function decode(cert) {
let file = randCertFileName(); // Get a random filename for out cert
var result;
fs.writeFile(file, cert, function(err) { // Write the cert to a file
if (err) {
console.error("Error writing certificate: " + error);
throw err;
} else {
execute('certutil -dump ' + file, function(out) { // Execute the certutil dump command
//console.log(out);
result = out;
fs.unlink(file); // Delete the certificate file
//return result;
});
}
});
}
});
私はconsole.log(out)
をすれば、私はそれ以外のダンプを取得することはできませんが、言ったように。 私はたくさんのSOの投稿を辿ってきましたが、私は見ることができるので、私はAJAX呼び出しを正しく行っていると思うので、問題はおそらく私のエンドポイントにあります。
ありがとうございます!
試みは、第2の機能のパラメータとしてコールバックをdecode' '与え、そして:あなたの結果を得ることができ、今
:あなたはそれを変更する必要があり、常にコールバックを返すことを忘れないでくださいコールバックをコールします。ここで、console.log(out)は結果を入力パラメータとして使用します – fzxt