SAP サーバーと、SAP から Web サービスを呼び出す Javascript があります。そのため、今まで CORS/JSONP を介して SAP Web サービスを呼び出していましたが、どちらもユーザー ID とパスワードのポップアップを取得する場所で機能しており、ユーザー ID とパスワードを入力するとうまくいきました。
ただし、コードからユーザー ID とパスワードを送信する必要があり、request.setHeader('Authorization', 'Basic' +btoa(username:password)) を実行すると、プリフライト 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' value = '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 サーバーがある場合にプリフライト OPTIONS リクエストを機能させる方法。サーバーファイルを変更する必要がありますか、それともコードを介して実現できますか?
親切に、助けてください!!!!!!!!!!!!!!
ありがとう