-2

screen shot of network panel

screen shot of console panel

i want to send form details to node.js server by using ajax function i am able see in console whatever i am sending

But i am not able to get any response from node.js server to html ajax function

server.js

var http = require('http');
http.createServer(function(request, response) {     
request.on('data', function (chunk) {   
    var res=chunk.toString('utf8');
    var obj=JSON.parse(res);
    var region=obj.region;
    var os=obj.os;              
    console.log(region);
    console.log(os);    
}); 
//var data="hi";
//how to send data
//response.send(data);
}).listen(8088);

client.html

<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
function myFunction() { 
var region = document.getElementById("region").value;
var os = document.getElementById("os").value;  
var data = {};
data.region = region;
data.os = os;
$.ajax({
 type: 'POST',
 jsonpCallback: "callback",
 datatype: 'jsonp',
 data: JSON.stringify(data),
 //contentType: 'application/json',
 url: 'http://127.0.0.1:8088/', //node.js server is running
 success: function(data) {
   alert("success");
   console.log(JSON.stringify(data));
 },
 error: function (xhr, status, error){
    console.log('Failure');
    alert("failure");
    },
 });
}
</script>
</head>
<body>
<form>
<input type="text" id="region" name="region"/>
<input type="text" id="os" name="os"/>
<input type="button" value="search" class="fil_search" onclick="myFunction()"/>
</form>
</body>
</html>

help me out from this how get response from node.js server. just i want see the alert box of success message in html page

Rapid
  • 111
  • 2
  • 13
  • Add a screenshot from `Network` panel in your developer tools. – Tomáš Zato Oct 26 '15 at 09:54
  • i am not able to attach the screen shot to this comment but the fallowing error i am getting " failure msg in alert box" and in console panel " XMLHttpRequest cannot load http://127.0.0.1:8088/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access." i am getting this error – Rapid Oct 26 '15 at 10:09
  • Attach screenshot to original post. You want help or not? If you do, can't you at least try to provide information for people to help you? I think it's the least you could do. – Tomáš Zato Oct 26 '15 at 10:10
  • i have attached the screen shot of network panel and console panel pls check and give me the solution – Rapid Oct 26 '15 at 10:26
  • Good job, I now know what caused your problem. You will have to follow one of the links I gave you - currently, it's impossible for XHR to work with your html and server. – Tomáš Zato Oct 26 '15 at 10:47

2 Answers2

1

You should add request.on('end') event which will be fired when all data is received and http request is completed:

data: Is triggered whenever body data comes in at the TCP socket level. Note that this doesn’t necessarily contain all the data, hence it’s called a chunk.

end: Is triggered when the HTTP request is completed. This indicates that all body data associated with it was read and the appropriate data events have been triggered.

var http = require('http');

http.createServer(function(request, response) {     
  request.on('data', function (chunk) {  
    console.log("Received body data", chunk.toString());
    // chunk is received, process it as you need
  }); 
  request.on('end', function() {
    // http request is competed, send response to client
    response.writeHead(200, "OK", {'Content-Type': 'text/html'});
    response.end();
  })
}).listen(8088);
Lisa Gagarina
  • 693
  • 5
  • 7
1

The reason why this happens to you is that you're loading the 127.0.0.1:8088 from a file:// protocol. This is an insecure suspicious operation for the browser and it doesn't allow it in order to protect you.

You have two options now:

  1. Enable CORS on your node.js - let the node.js server tell your browser that it's ok to load requests from diferent domains.
  2. Serve your client2.html from Node.js - instead of file://, you should get your client2.html from node js like from any other webserver - using URL path: 127.0.0.1:8088/client2.html.

Links I have provided contain solutions to your problem, which is a CORS error. There is not solution without properly configuring your Node.js script.

Community
  • 1
  • 1
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778