1

我有一个 SAP 服务器和一个从 SAP 调用 Web 服务的 Javascript。所以,到目前为止,我一直在通过 CORS/JSONP 调用 SAP Web 服务,并且两者都在工作,我将获得一个用户 ID 和密码弹出窗口,我将填写用户 ID 和密码,然后一切正常。

但是,我需要从代码中发送用户 ID 和密码,当我执行 request.setHeader('Authorization', 'Basic' +btoa(username:password)) 时,它会发送一个 Preflight 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 服务器,如何使 Preflighted OPTIONS Request 工作。我们是否需要更改任何服务器文件,或者可以通过代码来实现。

请大家帮忙!!!!!!!!!!!!!!!

谢谢苏拉夫

4

0 回答 0