0

The problem I am facing below, is only present when I upload the app into the server (Digital Ocean). When working at my local machine, I am able to make it works as the code stated below. Basicly, my jQuery Ajax request never is captured by my Node.js code. You will see that I am running the server by using express, however I have Nginx configured into the server too.

Node Set-Up:

var express = require('express');
var app = express();
var request = require("request");
var db = require('mysql');
const path = require('path');
var http = require("http").Server(app);
var io = require("/usr/local/lib/node_modules/socket.io").listen(http);

http.listen(3000, 'localhost'); //At the end of everything between here and the code.

GET Node Code:

app.use(express.static(__dirname + '/'));

app.get('/reqData',function(req,res){
    //I never arrive here
    console.log("print that I am here");
                transform(req.query.selection1,req.query.selection2,function(){
                res.json(data); //data is transformed globally  
                });

});

Ajax:

function requestData(){
     $.ajax({
            url: 'http://IPAddress/reqData',
            type: 'GET',
            data: {//Some stuff to send},
            dataType: "json",
            complete: function(data){

              do(data);
            },
            error: function(e){

              console.log(e);
            }
       });

    }

Console Log: Chrome Console Error

EDIT 2: I have edited the ip addres on ajax, using the server's ip. (without the port). I am also attaching the new errors logs (in chrome console) and the nginx configuration.

New Console Log Chrome Log

NGINX Config: Nginx Config 1 Nginx Config 2

  • Why do you call to `localhost`? you should call to the machine domain/IP, Attach the Nginx config, please – Michael Jun 12 '18 at 22:47
  • Your error says localhost, show the error you're getting when trying to access the app hosted in digital ocean please. – Marcos Casagrande Jun 12 '18 at 22:48
  • 1
    In your AJAX settings, use `url: '/reqData',` (it's client-side code, and with `localhost` it will try to connect to the user's machine, not the server) –  Jun 12 '18 at 22:50
  • Also you can't send post data in a 'GET' request – Mesar ali Jun 12 '18 at 22:54
  • Hi everyone, I have edited the IP address and attached the new logs and the nginx config. – Carlos Andrés Castro Marín Jun 12 '18 at 23:24
  • https://stackoverflow.com/questions/20035101/why-does-my-javascript-get-a-no-access-control-allow-origin-header-is-present – Christian Scott Jun 12 '18 at 23:43
  • Hi Christian, I does not matter if the app is in the same server from where the javascript code is requesting the data? My understanding here is that in order to use Ajax request to my app (despite everything is in the same server ) I have to use CORS? – Carlos Andrés Castro Marín Jun 12 '18 at 23:54
  • Hi everyone, it seems to be solved by adding a location in nginx config as "location /reqData" and the details about the proxy server, etc. I am not sure if it is the best way to send data to node, but it can find the path now. What do you think? – Carlos Andrés Castro Marín Jun 13 '18 at 00:50

0 Answers0