Problem receiving data usign http post

Hello! I have this error when execute my program:

“XMLHttpRequest cannot load http://192.168.1.100:8080/post. No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://localhost:8100’ is therefore not allowed access.”

My function is like this:

 makePostRequest() {
    this.http.post("http://192.168.1.100:8080/post", "Pepe&1234")
    .subscribe(data => {
        var alert = Alert.create({
            title: "Data String",
            subTitle: data.json().data,
            buttons: ["close"]
        });
        this.navController.present(alert);
    }, error => {
        console.log(JSON.stringify(error.json()));
    });
}

Somebody know what is the problem?
Thanks in advance.

Best regards!!

your ionic app is running on “http://localhost:8100”, and since that is a different domain than the resource you are trying to access, the server is rejecting your request.

The code for your ionic app is probably fine, the server code is likely where the issue lies. I can’t look at your server code (and it isn’t my area of expertise anyway), but this kind of error is a CORS error, so googling how to fix that would be a good place to start.

best of luck :slight_smile:

Thanks for replying Jeffroylance! :slight_smile:
The question is that I can have communication client - server, but not server - client so… I don’t know why I can send a data to my server but not receiving the response.

Here my server code:

http.createServer(function (req, res) {

if(req.method=='POST'){
	var body='';
	req.on('data', function (data){
		body +=data;
		Mi_cadena=data;
	});
	req.on('end',function(){
		var POST = qs.parse(body);
	});

	var client = new net.Socket();
	client.connect(1337, '192.168.1.101', function() {
		console.log('Connected');
		client.write(Mi_cadena);
	});

	client.on('data', function(data) {
		console.log('Received...: ' + data);
		res.end(data);
		client.destroy(); // kill client after server's response
	});

	client.on('close', function() {
		console.log('Connection closed');
	});
}

}).listen(8080, “192.168.1.100”);