[ACCEPTED]-how to use jQuery ajax calls with node.js-node.js
If your simple test page is located on other 11 protocol/domain/port than your hello world 10 node.js example you are doing cross-domain 9 requests and violating same origin policy therefore your jQuery 8 ajax calls (get and load) are failing silently. To 7 get this working cross-domain you should 6 use JSONP based format. For example node.js code:
var http = require('http');
http.createServer(function (req, res) {
console.log('request received');
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('_testcb(\'{"message": "Hello world!"}\')');
}).listen(8124);
and 5 client side JavaScript/jQuery:
$(document).ready(function() {
$.ajax({
url: 'http://192.168.1.103:8124/',
dataType: "jsonp",
jsonpCallback: "_testcb",
cache: false,
timeout: 5000,
success: function(data) {
$("#test").append(data);
},
error: function(jqXHR, textStatus, errorThrown) {
alert('error ' + textStatus + " " + errorThrown);
}
});
});
There are 4 also other ways how to get this working, for 3 example by setting up reverse proxy or build your web 2 application entirely with framework like 1 express.
Thanks to yojimbo for his answer. To add 7 to his sample, I wanted to use the jquery 6 method $.getJSON which puts a random callback 5 in the query string so I also wanted to 4 parse that out in the Node.js. I also wanted 3 to pass an object back and use the stringify 2 function.
This is my Client Side code.
$.getJSON("http://localhost:8124/dummy?action=dostuff&callback=?",
function(data){
alert(data);
},
function(jqXHR, textStatus, errorThrown) {
alert('error ' + textStatus + " " + errorThrown);
});
This 1 is my Server side Node.js
var http = require('http');
var querystring = require('querystring');
var url = require('url');
http.createServer(function (req, res) {
//grab the callback from the query string
var pquery = querystring.parse(url.parse(req.url).query);
var callback = (pquery.callback ? pquery.callback : '');
//we probably want to send an object back in response to the request
var returnObject = {message: "Hello World!"};
var returnObjectString = JSON.stringify(returnObject);
//push back the response including the callback shenanigans
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(callback + '(\'' + returnObjectString + '\')');
}).listen(8124);
I suppose your html page is hosted on a 3 different port. Same origin policy requires in most 2 browsers that the loaded file be on the 1 same port than the loading file.
Use something like the following on the 1 server side:
http.createServer(function (request, response) {
if (request.headers['x-requested-with'] == 'XMLHttpRequest') {
// handle async request
var u = url.parse(request.url, true); //not needed
response.writeHead(200, {'content-type':'text/json'})
response.end(JSON.stringify(some_array.slice(1, 10))) //send elements 1 to 10
} else {
// handle sync request (by server index.html)
if (request.url == '/') {
response.writeHead(200, {'content-type': 'text/html'})
util.pump(fs.createReadStream('index.html'), response)
}
else
{
// 404 error
}
}
}).listen(31337)
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.