在AngularJS中调用SOAP服务

时间:2015-12-18 10:36:22

标签: angularjs web-services soap

我使用以下代码调用SOAP服务:

angular.module('myApp', ['angularSoap'])

.factory("testService", ['$soap',function($soap) {
var base_url = "http://localhost/B1-MailManagement/MailManagementService.svc";

return {
    GetData: function(cnNumber){

        return $soap.post(base_url,"/", {cnNumber: "234423432423"});
    }
}
}])

.controller('mainController', function($scope, testService) {

$scope.track = function(){

    testService.GetData("898779879").then(function(response) {
        $scope.response = response;
    });        
}
})

我按照此链接执行此操作:SOAP Web Services in Angular and Ionic

但是,它不起作用。相反,它给出了这个错误:

XMLHttpRequest cannot load http://localhost/B1-MailManagement/MailManagementService.svc. Response for preflight has invalid HTTP status code 400

我还在Chrome中添加了CORS扩展程序,但似乎没有任何工作。

使用AngularJS调用SOAP服务的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

显然,尝试从Javascript调用SOAP服务并不明智。所以,我使用了restful服务,并在我的Global.asax文件中添加了以下代码。

protected void Application_BeginRequest(object sender, EventArgs e)
    {
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
        if( HttpContext.Current.Request.HttpMethod == "OPTIONS")
        {
            HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
            HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
            HttpContext.Current.Response.End();
        }
    }

并且有效。