Client side CORS issue
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 ?
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?
@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 I already have this proxy setup in my ionic project.
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.
@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.
Closing this issue, @lotus91 can reopen it if she is still getting it.