【发布时间】: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