Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

javascript - Node.js, Ajax sending and receiving Json

Using Ajax, I'm trying to just send Json data to a node server no processing involved just alerting when it's sent and alerting when it's received:

This is my html5: Simple button with an onclick function to trigger the function to use the ajax call

<!DOCTYPE HTML>
<html>
    <head>
        <script>
            function send()
            {
                //alert("Hello World");
                $.ajax
                ({
                    type: "post",
                    url: "http://localhost:8000", 
                    dataType: "json",
                    contentType: "application/json; charset=UTF-8",
                    data:  JSON.stringify({name: "Dennis", address: {city: "Dub", country: "IE"}})
                }).done(function ( data ) {alert("ajax callback response:" + data);
            });
        </script>
    </head>
    <body>
        <button onclick="send()">Click Me!</button>
    </body>
</html>

This is a portion of my node server: For creating a server and listening for certain actions

var port = 8000;
var server = http.createServer();
server.on('request', request);
server.listen(port);

function request(request, response) 
{
    var store = '';
    response.writeHead(200, {"Content-Type": "text/json"});
    request.on('data', function(data) 
    {
        store += data;
    });
    request.on('end', function() 
    {
       store = JSON.parse(store);
        console.log(store); 
        response.end(store);
    });
}  

No alerts are being fired so I don't think the ajax is attempting to send the information.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

try this on the server side:

var port = 8000;
var http = require("http");
var server = http.createServer();
server.on('request', request);
server.listen(port);
function request(request, response) {
    var store = '';

    request.on('data', function(data) 
    {
        store += data;
    });
    request.on('end', function() 
    {  console.log(store);
        response.setHeader("Content-Type", "text/json");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.end(store)
    });
 }  

and this on the client side:

$.ajax
({
  type: "POST",
  url: "http://localhost:8000",
  crossDomain:true, 
  dataType: "json",
  data:JSON.stringify({name: "Dennis", address: {city: "Dub", country: "IE"}})
 }).done(function ( data ) {
      alert("ajax callback response:"+JSON.stringify(data));
   })

Hope this works for you


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...