0

I'd like to send JSON data from the client to my Node.js server via jQuery and Ajax (websockets not possible) and get a JSON response back. I'd do that via POST, but since the Node.js server is running separately from the Apache server (which delivers my client-side website), jQuery is falling back to GET messages due to the same-origin-policy.

What's the best way to fix that?

KARTHIKEYAN.A
  • 18,210
  • 6
  • 124
  • 133
johnny
  • 8,696
  • 6
  • 25
  • 36
  • enable CORS on your server, or put the javascript on the node server. – mpm Feb 14 '13 at 16:50
  • 1
    Have a look at MDN's page on the [same-origin policy](https://developer.mozilla.org/en-US/docs/JavaScript/Same_origin_policy_for_JavaScript), especially "*Changing origin*" and [CORS](https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS). For the latter, also note [browser support](http://caniuse.com/#feat=cors). Otherwise, either implement a proxy script in the Apache server to pass messages between the browser and Node.js or consolidate to a single server/origin. – Jonathan Lonowski Feb 14 '13 at 16:52

1 Answers1

0

To enable CORS, add cors in node.js file:

var cors = require('cors');
app.use(cors());
GorvGoyl
  • 42,508
  • 29
  • 229
  • 225