iot-nodejs icon indicating copy to clipboard operation
iot-nodejs copied to clipboard

Client side CORS issue

Open lotus91 opened this issue 9 years ago • 6 comments

Hello, I am using the library in an ionic project. I'm including the bundle file in my index.html to use the API on client side, like mentioned in the documentation. The issue is when I call var deviceClient = new IBMIoTF.IotfApplication(WatsonIoT.settings); deviceClient.listAllDevicesOfType('Phone').then (function onSuccess (argument) { console.log("Success"); console.log(argument); }, function onError (argument) { console.log("Fail"); console.log(argument); });

The request fails and I get : XMLHttpRequest cannot load https://9pxax0.internetofthings.ibmcloud.com/api/v0002/device/types/Phone/devices. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.

I wanted to know if you have CORS enabled and if not will you consider adding it any sooner ? If there a solution to fix my issue beside creating proxy APIs to return JSON object ?

lotus91 avatar Mar 21 '17 15:03 lotus91

I have opened #66 to facilitate a solution here.

You will still need to use a proxy (the IoT Platform doesn't allow requests from unknown origins) but the "with-proxy" option on configuration would make it simpler to enable.

@sathipal could you review the PR?

bryancboyd avatar Mar 29 '17 22:03 bryancboyd

@lotus91 I am an Ionic novice, but here is an idea for a proxy setup:

http://ionicframework.com/docs/cli/test.html

In the ionic.project file you can add a property with an array of proxies you want to add. The proxies are object with two properties:

path: string that will be matched against the beginning of the incoming request URL. proxyUrl: a string with the url of where the proxied request should go.

{
  "name": "appname",
  "email": "",
  "app_id": "",
  "proxies": [
    {
      "path": "/",
      "proxyUrl": "https://<put your organization ID here>.internetofthings.ibmcloud.com"
    }
  ]
}

bryancboyd avatar Mar 29 '17 22:03 bryancboyd

@bryancboyd I already have this proxy setup in my ionic project.

lotus91 avatar Mar 30 '17 07:03 lotus91

Hi, I am getting this issue too when I am trying to call the Watson IOT api from a browser. However I can do the calls from a nodejs server application. I hope this helps.

adrianortuzar avatar Apr 30 '17 18:04 adrianortuzar

@lotus91 - Are you still seeing the Issue. https://github.com/ibm-watson-iot/iot-nodejs/pull/66 as mentioned by @bryancboyd has been successfully merged into the code. If you haven't tried it out after 31st of March, then can you please give a try and let us know, if you are still seeing the issue.

amprasanna avatar Jun 07 '17 06:06 amprasanna

Closing this issue, @lotus91 can reopen it if she is still getting it.

amitmangalvedkar avatar Jan 27 '18 08:01 amitmangalvedkar