1

I create a service for adding IOT devices to the list. When sending data I get an error:

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource

I tried like this:

xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = cfunc;
    xmlhttp.open("POST", url, true);
    xmlhttp.setRequestHeader("Authorization", "Basic " + auth);
    xmlhttp.setRequestHeader('Content-type', 'application/json');
    xmlhttp.setRequestHeader('Access-Control-Allow-Origin', '*');
    xmlhttp.setRequestHeader('Access-Control-Allow-Methods', 'POST, GET, DELETE, PUT');
    xmlhttp.setRequestHeader('Access-Control-Allow-Headers', 'x-requested-with,Content-Type');
    xmlhttp.send(JSON.stringify(jsonString));

but the error was repeating so I tried to do this, with the same error:

$.ajax({
    type: 'POST',
    url: 'https://pmhhdo.internetofthings.ibmcloud.com/api/v0002/bulk/devices/add',
    headers: {

        'Authorization': 'Basic ' + auth,
        'Content-Type':'application/json',
        'Access-Control-Allow-Origin': 'https://pmhhdo.internetofthings.ibmcloud.com',
        'Access-Control-Allow-Methods': 'GET, POST, PATCH, PUT, DELETE, OPTIONS',
        'Access-Control-Allow-Headers': 'Origin, Content-Type, X-Auth-Token'
    },


    withCredentials: true,
    crossDomain: true,
    data: jsonString,
    dataType: 'json',
    success : function(data) {
    },

});

Any ideas what I need to do?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
DENVOL
  • 11
  • 2
  • I have the same error, I've also tryied with 'jsonp' but in this link they said that 'jsonp' is not supported: https://cloud.ibm.com/docs/api-handbook?topic=api-handbook-format – Bruno Yuzo Apr 28 '20 at 13:39

1 Answers1

0

Looks like maybe you're missing "messaging" from the url.
Try https://orgId.messaging.internetofthings.ibmcloud.com

See documentation: https://console.ng.bluemix.net/docs/services/IoT/applications/api.html#api_link

ValerieLampkin
  • 2,620
  • 13
  • 27