Web Services 通过Web服务在React js中进行Soap调用

sg3maiej  于 2022-11-15  发布在  React
关注(0)|答案(1)|浏览(319)

我正在react上做一个应用程序,需要接收来自Web服务的一些数据。我已经有了soap请求和响应的示例,并且我已经完成了 Postman 的一些请求,它运行得很好。但是当我想在react上做soap请求和响应时,每次都给我带来同样的问题,即CORS阻止了我对XMLHttpRequest的访问

var xmlhttp = new XMLHttpRequest();
  xmlhttp.open('POST', 'https:/************/Geral.asmx?op=ObterLojasActivas', true);

  // build SOAP request
  var sr = '<?xml version="1.0" encoding="utf-8"?>' +
  '<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">' +
    '<soap12:Body>' +
      '<ObterLojasActivas xmlns="http://microsoft.com/webservices/" />' +
    '</soap12:Body>' +
  '</soap12:Envelope>;';

  xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState == 4) {
      if (xmlhttp.status == 200) {
        alert(xmlhttp.responseText);

      }
    }
  }
  // Send the POST request
  xmlhttp.setRequestHeader('Content-Type', 'text/xml');
  xmlhttp.send(sr);

}

function Login() {
  return (
    <div class="container-fluid" style={{
      backgroundImage: `url(franchising.jpg)`
    }}>
      <div className="login-content d-flex align-items-center" >
        <form className="form-signin mx-auto" onSubmit={soap}>
          <div className="text-center mb-4">
            <h1 className="h3 mb-3 font-weight-normal text-black font-weight-bold">Login</h1>
          </div>
          <input placeholder="Username" name='email' id="inputEmail" class="form-control my-4" />
          <input type="password" placeholder="Password" name='pass' id="inputPassword" class="form-control my-4" />
         <center><button className="btn btn-lg btn-block btn-login mb-5">Entrar</button></center>
        </form>
      </div>
    </div>
  );
}

export default Login;
aemubtdh

aemubtdh1#

我认为问题是您在标题请求中缺少所需的CORS权限,更多信息请访问:https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors/CORSMissingAllowOrigin
只需添加下面这行代码,它就能正常工作:

xmlhttp.setRequestHeader("Access-Control-Allow-Origin", "http://the-site-you-are-trying-to-send-the-request")

相关问题