我有一个SAP服务器和一个从SAP调用Web服务的Javascript。 所以,到现在为止,我通过CORS / JSONP调用SAP Web服务,两者都在工作,我会得到一个用户ID和密码弹出,我会填写用户ID和密码,事情会有效。
但是,我需要从代码中发送用户ID和密码,当我发出request.setHeader('授权','基本' + btoa(用户名:密码))时,它发送预检OPTIONS请求,我得到401错误(CORS预检错误)。
请查找随附的代码
function submitCRM() {
var url = "https://xxxx:8400/sap/bc/davey_rest_crm/zvertex_adr_cor?country=US&street=&city=tallmadge&state=oh&zipcode=44278";
var credentials = 'username:password';
var getJSON = function (url) {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
console.log(window.location);
console.log(window.location.origin);
xhr.open('GET', url, true);
xhr.responseType = 'application/json';
xhr.setRequestHeader('Authorization', 'Basic ' + btoa(credentials));
xhr.withCredentials = true;
xhr.onload = function () {
var status = xhr.status;
if (status == 200) {
resolve(xhr.response);
} else {
reject(status);
}
};
xhr.send();
});
};
getJSON(url).then(function (data) {
console.log(data);
});
}
在服务器端,我添加了以下参数: server-> response-> set_header_field(name =' Access-Control-Allow-Methods' 值=' GET,HEAD,OPTIONS,POST,PUT' )。
server->response->set_header_field( name = 'Cache-Control'
value = 'no-cache, no-store' ).
server->response->set_header_field( name = 'Pragma'
value = 'no-cache' ).
server->response->set_header_field( name = 'Access-Control-Allow-Origin'
value = 'https://localhost:44300' ).
server->response->set_header_field( name = 'Access-Control-Allow-Credentials'
value = 'true' ).
server->response->set_header_field( name = 'Access-Control-Allow-Headers'
value = 'Authorization,X-ACCESS_TOKEN,Access-Control-Allow-Headers,Origin,Accept,X-Requested-With,Content-Type,Access-Control-Request-Method,Access-Control-Request-Headers' ).
server->response->set_content_type( 'application/json' ).
如果我们有SAP服务器,如何使预检选项请求工作。我们是否需要更改任何服务器文件,或者可以通过代码实现。
请帮助,请!!!!!!!!!!!!!!
由于 Sourav