I have a page with two text areas, and I want to submit the data from one of them to node (using ajax), node then does some processing, and then returns the response which is then populated into the second text area.
The console log shows that the node code is working fine it gets to res.end() but the ajax keeps reporting an Error or a TimeoutError, instead of invoking the success callback method.
The key parts of the code are:
frontend
<script>
function processClick() {
var data = {};
data.topic = document.getElementById("topic").value;
data.message = document.getElementById("request_area").value;
$.ajax({
url: 'http://localhost:8888/',
dataType:'json',
type: 'POST',
data: JSON.stringify(data),
timeout: 2000,
success: function(data) {
$("#response_area").append(data);
},
error: function(jqXHR, textStatus, errorThrown) {
$("#response_area").append('[Error[textstatus=' + textStatus + ',errorThrown=' + errorThrown + ']]');
}
});
}
</script>
Node
var port = 8888;
var http = require('http');
http.createServer(function (req, res) {
response = null;
console.log('request received');
res.writeHead(200, {'Content-Type': 'application/json'});
res.end('{a:"b"}');
}).listen(port);
console.log("listening on " + port);
I've stripped out most of what node does, and tried to get it just to return a json string {a:"b"} but that too results in errors. Am I messing up the content types? How much of an impact would that have?
Any help would be appreciated.