javascript - 在 AngularJS 框架范围内的页面加载时调用 Web 服务

标签 javascript html ajax angularjs web-services

我有一个返回 Jsonobject 的 Web 服务,我使用 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()"  >

网络服务进入错误步骤并抛出该错误:

"\n\n\nError 404--Not Found\n\n\n\n\n\n

Error 404--Not Found

\n\n\n

From RFC 2068 Hypertext Transfer Protocol -- HTTP/1.1:

\n10.4.5 404 Not Found\n

The server has not found anything matching the Request-URI. No indication is given of whether the condition is temporary or permanent.

If the server does not wish to make this information available to the client, the status code 403 (Forbidden) can be used instead. The 410 (Gone) status code SHOULD be used if the server knows, through some internally configurable mechanism, that an old resource is permanently unavailable and has no forwarding address.

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

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

最佳答案

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

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

关于javascript - 在 AngularJS 框架范围内的页面加载时调用 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30192770/

相关文章:

javascript - 如何使用 jQuery 将 HTMLElement 转换为字符串,将所有 css 放入样式标签中

javascript - 在导出到 ui-grid 中的 Excel 期间设置单元格样式

javascript - 使用 jquery 将文本字段聚焦在其值之后

html - 使用文本装饰颜色

javascript - HTML5 文件 API 按顺序包含多个文件,而不是一次全部文件

jquery - 让 ASP.NEt 函数与 Ajax 一起使用来填充下拉列表

javascript - 检索包含大写和小写字母的复选框值

html - 响应式 CSS 网格不允许适当的间距

java - 使用通过 JSON 和 jQuery 传递的数据在下拉列表中创建两个值

jquery - django 表单和 ajax