【问题标题】:How to call web service from AngularJS, passing it xml and authentication data如何从 AngularJS 调用 Web 服务,传递 xml 和身份验证数据
【发布时间】:2017-03-28 13:35:24
【问题描述】:

我需要调用一个 Web 服务并将 xml 和身份验证数据传递给它。到目前为止,这是我想出的:

var xml ="<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'>"
+ "<soapenv:Header/>"
+ "<soapenv:Body>"
    + "..."
    + "</soapenv:Body></soapenv:Envelope>";


var config = {
            method: "POST",
            url: 'http://....wsdl',
            data: xml
        };
        $http(config).
            then(function (data, status, xhr) {
                $scope.MyID = data;
            }, function errorCallback(xhr) {
                //print error to console.
                console.log(xhr.responseText);
            });

我需要弄清楚如何传递用户名和密码。而且上面也给了我一个错误:

XMLHttpRequest 无法加载 http://....wsdl。对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin 'http://localhost:' 因此不允许访问。

奇怪的是,当我以同样的方式从 .NET 调用服务时,access-control-allow-origin 没有问题

【问题讨论】:

    标签: angularjs web-services


    【解决方案1】:

    由于您使用 angular 进行 http 重新查询,因此请使用以下任何代码来通过身份验证。

    app.run(['$http', function($http) {
        $http.defaults.headers.common['Authorization'] = /* ... */;
    }]);
    
    app.config(['$httpProvider', function($httpProvider) {
        $httpProvider.defaults.headers.common['Authorization'] = /* ... */;
    }])
    

    试试这个库希望对你有帮助

    https://github.com/andrewmcgivery/angular-soap

    【讨论】:

    猜你喜欢
    • 2011-01-14
    • 1970-01-01
    • 2014-12-13
    • 2018-05-12
    • 1970-01-01
    • 2016-06-10
    • 1970-01-01
    • 2016-08-20
    • 1970-01-01
    相关资源
    最近更新 更多