我创建了一项用于将 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) {
},
});
有什么想法我需要做什么吗?