I'm trying to allow javascript to communicate with a Node.js server.
POST request (web browser)
var http = new XMLHttpRequest();
var params = "text=stuff";
http.open("POST", "http://someurl.net:8080", true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
alert(http.onreadystatechange);
http.onreadystatechange = function() {
if (http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(params);
Right now the Node.js server code looks like this. Before it was used for GET requests. I'm not sure how to make it work with POST requests.
Server (Node.js)
var server = http.createServer(function (request, response) {
var queryData = url.parse(request.url, true).query;
if (queryData.text) {
convert('engfemale1', queryData.text, response);
response.writeHead(200, {
'Content-Type': 'audio/mp3',
'Content-Disposition': 'attachment; filename="tts.mp3"'
});
}
else {
response.end('No text to convert.');
}
}).listen(8080);
Thanks in advance for your help.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…