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

发布于 2025-01-11 08:15:47 字数 1899 浏览 0 评论 0原文

我正在做一个反应应用程序,我需要接收来自网络服务的一些数据。我已经有了肥皂请求和响应的样本,并且我已经完成了邮递员的一些请求,并且工作正常。但是,当我想在 React 上发出肥皂请求和响应时,每次 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;

I am doing an application on react where i need to receive some data coming from webservice. I already have the sample of soap request and response, and i already had done some request by the postman and it is working fine. But when i want to make a soap request and response on react, it ives me the same problem every time that is CORS has blocked my access to 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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画骨成沙 2025-01-18 08:15:47

我认为问题是您在标头请求中缺少所需的 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")

I think the problem is you are missing the required CORS permission in your header request, more info here: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors/CORSMissingAllowOrigin

Simply add this line and it should work fine:

xmlhttp.setRequestHeader("Access-Control-Allow-Origin", "http://the-site-you-are-trying-to-send-the-request")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文