1

我创建了一项用于将 IOT 设备添加到列表的服务。发送数据时出现错误:

对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头

我试过这样:

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));

但错误正在重复,所以我尝试这样做,但出现同样的错误:

$.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) {
    },

});

有什么想法我需要做什么吗?

4

1 回答 1

0

看起来您可能缺少网址中的“消息”。
试试https://orgId.messaging.internetofthings.ibmcloud.com

请参阅文档:https ://console.ng.bluemix.net/docs/services/IoT/applications/api.html#api_link

于 2017-05-19T20:59:19.800 回答