Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I send an AJAX request to a node.js server via HTTPS?
    text
    copied!<p>I have the following node.js server set up listening to port 9001</p> <pre><code>var https = require('https'); var fs = require('fs'); var qs = require('querystring'); var options = { key: fs.readFileSync('privatekey.pem'), cert: fs.readFileSync('certificate.pem') }; https.createServer(options, function (req, res) { res.writeHead(200); console.log('Request Received!'); console.log(req.method); if (true || req.method == 'POST') { var body = ''; req.on('data', function (data) { body += data; }); req.on('end', function () { console.log(body); var POST = qs.parse(body); console.log(POST); }); } res.end("hello, world\n"); }).listen(9001); </code></pre> <p>and I am trying to get this server to respond to an AJAX call </p> <pre><code>function form_save() { console.log("submitted!"); var data_obj = { data1: "item1", data2: "item2" } $.ajax({ url: 'https://adam.testserver.com:9001/', type: "POST", dataType: "json", data: data_obj, success: function() { console.log("success!"); }, complete: function() { console.log("complete!"); } }); } </code></pre> <p>There are two problems occurring with my arrangement. The first is that if I start the server and then click the button that triggers my form_save() the node server does not respond and I get the following error:</p> <pre><code>submitted! OPTIONS https://adam.testserver.com:9001/ Resource failed to load jQuery.extend.ajaxjquery.js:3633 $.ajaxjquery.validate.js:1087 form_savew_worksheet.php:64 confirm_deletew_worksheet.php:95 jQuery.event.handlejquery.js:2693 jQuery.event.add.handlejquery.js:2468 w_worksheet.php:73 complete! </code></pre> <p>At this point if I access that url directy (https://adam.testserver.com:9001/) I get the expected "hello, world" output as well as the console message "Request Received! GET". From this point on if I click the button to trigger my AJAX call I get a new error.</p> <pre><code>submitted! XMLHttpRequest cannot load https://adam.testserver.com:9001/. Origin https://adam.testserver.com is not allowed by Access-Control-Allow-Origin. w_worksheet.php:73 complete! </code></pre> <p>I don't understand why I get this message as both my form and node server reside on the same server. Thanks for taking the time to read, I appreciate any help I can get on this. I've been stuck for a while now!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload