0

SERVER : cloud server, Ubuntu 15.04 x64. nginx is installed listening port 80.

SERVER program : written using NodeJS, simply response in json. like { msg : "done" }

Client (HTML page using React and JQuery) : simple ask the server for the json data.

The problem is on client

// 1)
this.serverRequest = $.get({url:'https://api.github.com/users/octocat/gists', success:function(ret) {
// 2)
//this.serverRequest = $.get({url:'http://my_server_ip_address:7534/test', success:function(ret) {
      alert("ok done");
    }.bind(this), error:function(e) {
      alert("Error : " + e);
    }.bind(this)});

I have no problem with 1) request. I do receive some data. but with 2) request, 'error' function is called and executing 'alert('Error :' + e);' only shows 'Error : [object Object]'. It does not show any useful information..

So I thought my nodejs server program has some problems and I went on searching for answers. I think I try all possible answers..

1) setting iptable to accept port 7534. 2) making nodejs server to listen on 0.0.0.0 3) using res.json().. res.jsonp()

I can see {msg : "done"} if I try with Chrome. so I guess my nodejs server works.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ji Youn
  • 3
  • 4
  • Is the interface using https? Do you see any errors in your JavaScript console (F12)? – blex Feb 25 '16 at 19:46
  • First, you receive error message `Error: [object Object ] ` because it's base types transformation in js. Second, in nginx maybe install firewall, but don't sure, such as you received right data in Chrome. What browser you use when receive a wrong data – Dmytro Feb 25 '16 at 20:42
  • no it does use https. it uses http. – Ji Youn Feb 26 '16 at 02:05
  • blex : no it does use **https**. it uses **http**. There is no error shown in console. Dmitry : all browsers receive the data correctly. I use Chrome to open my client html file which contains javascript codes. – Ji Youn Feb 26 '16 at 02:10
  • `Error : [object Object]` because e is an object. And `{msg : "done"}` is not a json its an object. – Rajshekar Reddy Feb 26 '16 at 02:18

2 Answers2

0

Look the message of error. Instead of alert(e) use console.log(e), for see properties of object e.

Juven_v
  • 166
  • 2
  • 8
  • ah...man. why didn't i check the console in the first place.. (I didn't know how to check anyway) Yes there is a comment. net::ERR_CONNECTION_REFUSED. hm...now I need to configure my server setting. I dont know how yet =P THANK YOU – Ji Youn Feb 26 '16 at 02:20
0

ok following @Juven_v 's answer..I figured the 'object' was Error object. And I was able to see the error message through console.log(e).

Here is summary of the solution I found: 1) When the server program responses, put header **Access-Control-Allow-Origin with value *.

I am using nodeJS with express so the code is

res.header('Access-Control-Allow-Origin', '*');

And the client requests with the url. I use JQuery and the code is

$.get({ url: "http://address!!!!", type: "GET", success: function(result) { console.log("yay"); console.log(result); }, error: function (err) { console.log(err); }, });

If the above client code does not work, then try to add 'dataType: "json"'

2) If I add the following code in the server program.. res.header('Access-Control-Allow-Headers', 'Content-type');

then the client request must have //contentType: "application/json",

References : How does Access-Control-Allow-Origin header work?

jQuery AJAX to node server throws net::ERR_CONNECTION_REFUSED

net::ERR_CONNECTION_REFUSED Error jQuery ajax node.js

THANK YOU ALL!!!!

Community
  • 1
  • 1
Ji Youn
  • 3
  • 4