0

I successfully made an ajax call to a endpoint that I set up. I send it a raw certificate as JSON, and the backend decodes it. Now, I can bring up the decoded result with a console.log but I can't figure out how to return it as the result.

Ajax Call:

    $("#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 Endpoint:

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;

                });
            }
        });

    }
});

As I said, if I do a console.log(out) I can get the dump, but not otherwise. I've trawled thru many SO posts and from what I can see, I think I'm doing the AJAX call correctly, so the problem is probably in my endpoint.

Thanks in advance!

hamza765
  • 106
  • 14
  • 1
    try giving `decode` a callback as the second function parameter, and call the callback where console.log(out) is with the result as an input parameter –  Jun 13 '16 at 23:45

1 Answers1

1

In your nodejs end point, you are calling decode function with two arguments, first the body, then a callback. But your function declaration only take one argument. You must change that and don't forget to always return the callback :

function decode(cert, callback) {
    let file = randCertFileName();
    var result;
    fs.writeFile(file, cert, function(err) {
        if (err) {
            console.error("Error writing certificate: " + error);
            return callback(err);
        }

        execute('certutil -dump ' + file, function(out) {
            result = out;
            fs.unlink(file);

            return callback(null, result); // first argument is err
        });
    });
}

now, you can get your result :

.post(function(req, res) {
    const body = req.body.body; 
    decode(body, function(err, result) { // don't forget the two arguments,
                                         // first error, then result.
        if (err)
          throw err;
        res.json(result);
    });
 });
Jérôme
  • 1,060
  • 1
  • 7
  • 18