【发布时间】:2018-01-16 08:21:24
【问题描述】:
这个 CORS 问题陷入疯狂,没有解决方案。我正在 SAP HANA 中使用以下 xsaccess 文件内容创建 XSODOTA 服务,并在我的本地主机中使用 UI5 应用程序。
{
"exposed": true,
"authentication": [{
"method": "Basic"
}],
"mime_mapping": [{
"extension": "jpg",
"mimetype": "image/jpeg"
}],
"force_ssl": false,
"enable_etags": true,
"prevent_xsrf": true,
"anonymous_connection": null,
"cors": [{
"enabled": true,
"allowMethods": [
"GET",
"POST",
"HEAD",
"OPTIONS"
],
"allowOrigin": "*",
"maxAge": "3600"
}],
"headers": {
"enabled": true,
},
"cache_control": "no-cache, no-store",
"default_file": "index.html"
}
我已经完成了所有这些,我可以从 XS 管理工具中的 SAP 论坛收集如下:
在jQuery ajax函数中调用服务如下:
$.ajax({
type: "GET",
url: "odataurl?$format=json",
data: null,
contentType: "application/json; charset=utf-8",
jsonpCallback: 'processJSON',
headers : {
"Access-Control-Allow-Origin" : "*"
},
crossDomain: true,
xhrFields: {
withCredentials: true
},
success: function(msg) {
console.log(msg.d.results);
that.onbindTable(msg.d.results);
},
error: function(err) {
}
});
无法加载
http://odataurl?$format=json:对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin 'http://localhost:20019' 因此不允许访问。
【问题讨论】:
-
尝试在 cors 配置中将
"allowOrigin": "*"替换为"allowOrigin": ["*"]。 “Access-Control-Allow-Origin”标头应出现在服务器响应中,您不必将其放入请求中。看看这个答案:https://answers.sap.com/questions/65623/cors-issue-accessing-xsodata.html