【问题标题】:SOAP Service fails with AJAX call but works fine in SOAPUISOAP 服务因 AJAX 调用而失败,但在 SOAPUI 中工作正常
【发布时间】:2017-07-26 08:01:28
【问题描述】:

调用soap 服务会引发404 错误,但在SOAP UI 中可以正常工作。我发现这可能是由于 CORS 问题,所以我将标题设置为 'Access-Control-Allow-Origin': '*' 但没有运气

以下是我如何调用服务

let _this=this;
  var webServiceURL = 'http://localhost:8082/ode/processes/WS_Invocation.WS_InvocationPort';


var soapMessage = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.invocation.tps"><soapenv:Header/><soapenv:Body><ws:WS_InvocationRequest><ws:input>1</ws:input><ws:input1>Mohan</ws:input1></ws:WS_InvocationRequest></soapenv:Body></soapenv:Envelope>'

  $.ajax({
        url: _this.webServiceURL, 
        type: "POST",
        dataType: "xml", 
        crossDomain: true,
        headers: {
                'Access-Control-Allow-Origin': '*'
            },
        data: _this.soapMessage, 
        contentType: "text/xml; charset=\"utf-8\"",
        success: _this.OnSuccess, 
        error: _this.OnError
    });

以下是来自 SOAP UI 的成功响应

【问题讨论】:

  • 从您的 $.ajax 呼叫中删除 headers: { 'Access-Control-Allow-Origin': '*' }。 Access-Control-Allow-Origin 是服务器必须发送的 response 标头。将其作为请求标头发送不会为您解决任何问题。删除后,要获得更多帮助,请使用 stackoverflow.com/posts/45309437/edit 编辑/更新您的问题,并添加您在浏览器开发工具控制台中看到的确切错误消息。

标签: jquery ajax soap cors


【解决方案1】:

这是 CORS 问题,您应该在 API 服务中启用 CORS,每个平台都有不同的配置,例如 NodeJS、Apache、Nginx 等。

您可以在此处查看有关如何在 Web 服务中激活 CORS 的文档https://enable-cors.org/index.html

对于请求,您可以做这样的事情

//jQuery <= 1.12.x
$.get(API_URL)
.success(function(response) {
    var res = $(response).find('YOUR_NODE');
})
.error(function(err) {

});

//jQuery > 1.12.x
$.get(API_URL)
.then(function(response) {
  var res = $(response).find('YOUR_NODE');
})
.catch(function(err) {

});

问候。

【讨论】:

  • 我已经通过设置标题“Access-Control-Allow-Origin”启用了 CORS:“*”。这还不够吗?我还从浏览器控制台看到 responseText 显示 "Cannot POST".
  • @Mohan 这意味着您的路由器设置未设置为 POST?你在用什么? ASP.NET?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-15
  • 1970-01-01
  • 2018-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-12
相关资源
最近更新 更多