我无法使用Angular javascript从Web服务获得响应

时间:2014-12-02 04:53:19

标签: angularjs

var msg = document.getElementById('inputXML')。innerHTML;如何将输入xml作为参数传递给Web服务,并使用html中的angular javascript显示来自Web服务的响应。 这是我的代码,请帮助,我无法从Web服务获得响应。

<div ng-app="customerApp" ng-controller="customersController">

    <ul>
        HI<br><br><li ng-repeat="x in names">{{x}}</li>
    </ul>

</div>

<script>
var app = angular.module('customerApp');

app.factory(
        "setAsXMLPost",
        function() {
            //prepare the request data for the form post.
            function transformRequest(data, getHeaders) {
                var headers = getHeaders();
              headers[ "Content-type" ] = "text/xml; charset=utf-8";
                // using parsexml for xml
                return(jQuery.parseXML(data));
            }
            return(transformRequest);
        }
);




    function customersController($scope, $http, setAsXMLPost) {
        var msg = document.getElementById('inputXML').innerHTML;
        $http.post("url.asmx", msg, {transformRequest: setAsXMLPost}).success(function(response) {
                    $scope.names = response;
                });
    }
</script>
<div id="inputXML">
    <ACORD> <SignonRq> <UserId>CUser</UserId> <Password>XuViDgegi/KtGyJuXfuMrw==</Password>
    <SignonPswd> <CustId> < </ACORD>
</div>

4 个答案:

答案 0 :(得分:0)

我不确定xml但你没有发布它,使用innerHTML<div id="inputXML">获取实际的xml。

inputXML : msg.innerHTML // use innerHTML to get the actual xml

答案 1 :(得分:0)

因为您没有检索div的内容。您只是从DOM树中提取#inputXML节点。你可以试试

var msg = document.getElementById('inputXML').innerHTML;

答案 2 :(得分:0)

在var msg中你得到一个javascript对象虽然你需要完整的html来处理所以它应该是

var = document.getElementById('inputXML').innerHTML;

默认情况下,角度js中$ http的tranformRequest是json你必须将它改成text / xml我写了一篇关于你如何改变它的文章tranformRequest example angularjs

完整的例子,文件名是test.php放在root

<?php
if ( $_SERVER['REQUEST_METHOD'] === 'POST' ){
    $xmlData = file_get_contents('php://input');
    header("Content-type: text/xml; charset=utf-8");
    echo '<?xml version="1.0" encoding="utf-8"?>' . $xmlData;
    exit;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<body >
<title>Simple xml post in angularjs Web Application</title>
<meta name="Description" content="Simple xml post in angularjs Web Application">
    <div ng-app="customerApp" ng-controller="customersController">
        <ul>
            <li ng-repeat="x in users">{{x}}</li>
        </ul>
    </div>
    <div id="inputXML">
    <ACORD>
        <SignonRq><UserId>CUser1</UserId><Password>XuViDgegi/KtGyJuXfuMrw==</Password></SignonRq>
        <SignonRq><UserId>CUser2</UserId><Password>XuViDgegi/KtGyJuXfuMrw==1</Password></SignonRq>
    </ACORD>
</div>    <!-- Libraries -->
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.3/angular-route.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
var app = angular.module('customerApp', []);

app.factory(
        "setAsXMLPost",
        function() {
            //prepare the request data for the form post.
            function transformRequest(data, getHeaders) {
                var headers = getHeaders();
                headers[ "Content-type" ] = "text/xml; charset=utf-8";
                // using parsexml for xml
                return(jQuery.parseXML(data));
            }
            return(transformRequest);
        }
);

app.factory(
        "getAsXML",
        function() {
            //prepare the request data for the form post.
            function transformResponse(data, getHeaders) {
                var headers = getHeaders();
                headers[ "Content-type" ] = "text/xml; charset=utf-8";
                // using parsexml for xml
                return(jQuery.parseXML(data));
            }
            return(transformResponse);
        }
);
function customersController($scope, $http, setAsXMLPost, getAsXML) {
    var msg = document.getElementById('inputXML').innerHTML;
    $scope.users = [];
    $http.post("test.php", msg, {transformRequest: setAsXMLPost, transformResponse: getAsXML}).success(function(returnedXMLResponse) {
        //here you will get xml object in reponse in returnedXMLResponse
    });
}
</script>
</body>
</html>

答案 3 :(得分:0)

您是否检查过您的控制器是否正确执行,我认为标准控制器声明将是这样的:

.controller('customersController', ['$scope', '$http', 'setAsXMLPost',function($scope, $http, setAsXMLPost){
    var msg = document.getElementById('inputXML').innerHTML;
    $http.post("http://nordevd208wa1x.csc-fsg.com/TPOServiceEnh7/TPOService/TPOService.asmx", msg, {transformRequest: setAsXMLPost}).success(function(response) {
        $scope.names = response;
    });
}]);