Answer the question
In order to leave comments, you need to log in
How to respond to a request sent from ajax in Node js?
I'm trying to send a get request to a node js server using ajax. The request comes to the server, but the response from the server does not reach, giving the error "Not connect. Verify Network." If the same request is entered in the search bar, the response to the browser comes.
Server request code:
$.ajax({
url: 'http://127.0.0.1:8888/1',
data: {'q':'1'},
success: function(msg){
alert( "Data Saved: " + msg );
},
error: function(jqXHR, exception) {
if (jqXHR.status === 0) {
alert('Not connect.\n Verify Network.');
} else if (jqXHR.status == 404) {
alert('Requested page not found. [404]');
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.');
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax request aborted.');
} else {
alert('Uncaught Error.\n' + jqXHR.responseText);
}
}
});
var express = require('express');
var server = express();
server.route('/1')
.get(function(req, res) {
var body = JSON.stringify("Success!");
res.json(body);
})
server.listen(8888);
Answer the question
In order to leave comments, you need to log in
Let me guess, the site itself is hanging on a different host?
You need to implement CORS on the server
AND learn how to use developer tools in the browser, there is a lot of useful stuff
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question