【问题标题】:Call webservice at page load in AngularJS Framework scope在 AngularJS 框架范围内的页面加载时调用 web 服务
【发布时间】:2015-07-23 10:32:15
【问题描述】:

我有一个返回 Jsonobject 的网络服务,我使用 Ajax 在 JS 中调用它,如下所示:

$scope.init = function () {
                $.ajax({
                    type: 'GET',
                    timeout: 1000000000,
                    url: serverName.replace('services', 'servicesV2') + '/getTreasuryResults',
                    data: { data: 'None' }, // the data in form-encoded format, ie as it would appear on a querystring
                    dataType: "text", // the data type we want back, so text.  The data will come wrapped in xml
                    success: function (data) {
                        var dataAngularTreasury = JSON.parse(data);
                        alert(dataAngularTreasury);
                        //FillDefaultTreasuryValues(data.split(";"));
                        CallTreasuryDetailValuesWS(934);
                    },
                    error: function (data) {
                        alert(errServiceCall); // show the string that was returned, this will be the data inside the xml wrapper
                        $('#loading').hide();
                    }
                });
            };

如果我在 ng-click 中调用该 init() 函数

<button id="btnGetDefaultValues" type="button" class="button" ng-click="init()">Fill</button>

它运行没有问题。

但是如果我在页面加载中调用这个网络服务,比如

angular.module('termDeposite', []).controller('userCtrl', function ($scope) {

    $scope.treasuryValues = [];


    angular.element(document).ready(function () {
        $.ajax({
            type: 'GET',
            timeout: 1000000000,
            url: serverName.replace('services', 'servicesV2') + '/getTreasuryResults',
            data: { data: 'None' }, // the data in form-encoded format, ie as it would appear on a querystring
            dataType: "text", // the data type we want back, so text.  The data will come wrapped in xml
            success: function (data) {
                var dataAngularTreasury = JSON.parse(data);
                alert(dataAngularTreasury);
                //FillDefaultTreasuryValues(data.split(";"));
                CallTreasuryDetailValuesWS(934);
            },
            error: function (data) {
                alert(errServiceCall); // show the string that was returned, this will be the data inside the xml wrapper
                $('#loading').hide();
            }
        });
    });
});

如果我在 ng-init 触发器中调用这个 web 服务,比如

<body id="body" ng-app="termDeposite" ng-controller="userCtrl" ng-init="init()"  >

webservice 进入错误步骤并抛出该错误:

"\n\n\n错误 404--不是 找到\n\n\n\n\n\n

错误 404--不是 找到

\n\n\n

来自 RFC 2068 超文本传输​​协议 -- HTTP/1.1:

\n10.4.5 404 Not Found\n

服务器没有找到任何匹配的 请求-URI。没有说明条件是否是 临时或永久。

如果服务器不希望这样做 客户端可用的信息,状态码 403(禁止) 可以代替使用。应该使用 410 (Gone) 状态码,如果 服务器通过一些内部可配置的机制知道, 旧资源永久不可用且无转发 地址。

\n\n\n\n\n\n"

最后,我可以使用 ng-click 调用 web 服务,但我不能使用 ng-init 或 pageload 调用相同的 web 服务。那么如何在页面初始化中使用 ajax 或在 Angular 框架范围内使用页面加载来调用 Web 服务?

【问题讨论】:

  • 使用您的浏览器网络选项卡或提琴手检查。他们提出完全相同的要求吗?
  • 是的,我可以使用复制完整的 ws url 调用相同的 web 服务并将其直接粘贴到浏览器选项卡,它不会返回任何错误。
  • 在控制器中使用 .ready 没有多大意义
  • 但我需要 ws 在页面加载时返回数据@YOU

标签: javascript html ajax angularjs web-services


【解决方案1】:

假设您将 serverName 作为全局变量并且它是可读的,那么 ng-init 版本或创建自定义指令并将代码粘贴到链接函数中应该可以工作。

在 Chrome 开发工具(内置于 chrome)的网络选项卡中检查实际调用的 URL - cmd+alt+j on mac f12 on PC。

【讨论】:

  • 这是关于 serverName 的。谢谢@alexrogins
猜你喜欢
  • 1970-01-01
  • 2013-03-20
  • 2018-03-22
  • 1970-01-01
  • 2018-05-04
  • 1970-01-01
  • 1970-01-01
  • 2020-05-23
  • 1970-01-01
相关资源
最近更新 更多