node.js中ajax-client和https-server之间的通信

时间:2013-11-26 15:45:26

标签: javascript ajax node.js https ssl-certificate

如何在Ajax和nods.js中的https-server之间进行通信。我不知道客户端应用程序中的错误在哪里?问题是服务器没有通过https从ajax-client接收请求!我最终是否嵌入了客户证书?非常感谢你的帮助!

**client**

function sendData()
{
var xmlhttp;    

xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST","https://192.168.437.1:8000", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.onreadystatechange=function(){
 if (xmlhttp.readyState==4 && xmlhttp.status==200){

        var message = xmlhttp.responseText;
 }
 }
 xmlhttp.send("Hello World");

 }// end send Data

服务器

var https = require('https');
var fs = require('fs');

var options = {
key: fs.readFileSync('certs/key.pem'),
cert: fs.readFileSync('certs/cert.pem')
};

https.createServer(options, function (req, res) {
var message;
req.on('data', function (chunk) {
message += chunk; 
});
req.on('end', function () {
message = message.toString('utf8');
res.writeHead(200, {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Method": "POST, GET, OPTIONS",
"Access-Control-Allow-Headers": req.headers["access-control-request-headers"]
 });
res.end(message);
});

}).listen(8000, '192.168.437.1');

0 个答案:

没有答案