2

I have tested the Hello world program with Node.js concept and it is working fine.

File details:

index.html
socket.js

run in command prompt : node socket.js

And i have tried the ajax call in node.js using same hello world files and follow the code in the following link,

how to use jQuery ajax calls with node.js

server.js

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(8080);

index.html

<script src="/socket.io/socket.io.js"></script>
<script>
  $(document).ready(function() {
    $.ajax({
        url: 'http://localhost:8080/',
        dataType: "jsonp",
        jsonpCallback: "_testcb",
        cache: false,
        timeout: 5000,
        success: function(data) {
            $("#test").append(data);
        },
        error: function(jqXHR, textStatus, errorThrown) {
            alert('error ' + textStatus + " " + errorThrown);
        }
    });
});

</script>
<div id="test"></div>

And i run with the following method in the command prompt

node node.js

And run in the browser like,

http://localhost:8080

Then, It provides the following output

_testcb('{"message": "Hello world!"}')

But i cant get the proper result, Please explain me, how to integrate the ajax in node.js with sample code?

Thanks for your help.

Community
  • 1
  • 1
Sivanath
  • 459
  • 1
  • 5
  • 12
  • could you paste your current codes here? We can have a better question then. – footy Nov 21 '11 at 13:22
  • I have following the code from this link http://stackoverflow.com/questions/5373987/how-to-use-jquery-ajax-calls-with-node-js/5376131#5376131 – Sivanath Nov 21 '11 at 13:24
  • I ment, the code *you* have done till now. The details in `index.html` and `socket.js` files – footy Nov 21 '11 at 13:27
  • Please check now my description. Thanks for your help. – Sivanath Nov 21 '11 at 13:34
  • I might be wrong, but I don't think jQuery and node.js is a very good match. jQuery like most js libraries depend on things like the window object and other built'ins that are always present in browsers, and thus the code isn't hardened to test for their existence. Caveat: i have zero experience with Node.js – Martin Jespersen Nov 21 '11 at 13:41
  • I think the code is OK. I really don't understand why do you include socket.io if you don't use it. – J. K. Nov 21 '11 at 14:08
  • @MartinJespersen - the jQuery is running in the browser, making an ajax call to a webserver that happens to be running under node.js. The fact that they are both JavaScript is not relevant: the question is how to create the (text) output on the server such that it will work as JSONP from the client ajax call. – nnnnnn Nov 21 '11 at 14:10

3 Answers3

0

Change your writeHead function to output JSON instead of plain text:

res.writeHead(200, {'Content-Type': 'application/json'});
Clint
  • 2,871
  • 2
  • 25
  • 28
0

Note that success and error are deprecated in jQuery. Use .then and similar promise/defered-related methods instead.

Also, use express NPM library instead of plain node.js web server - it does most of this work on sending and receiving JSON on server for you.

nponeccop
  • 13,527
  • 1
  • 44
  • 106
0

I have tried to implement simple AJAX in nodeJS and this is the link: nodesamples

Hope it helps :)

Saravanan S
  • 1,021
  • 1
  • 10
  • 24