$ http get不会在AngularJS中抛出成功或错误[重复]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了$ http get不会在AngularJS中抛出成功或错误[重复]相关的知识,希望对你有一定的参考价值。
以下是GET的WCF RESTful服务声明
[OperationContract]
[WebGet(UriTemplate= "MemberSearch/{MemberName}", ResponseFormat=WebMessageFormat.Json,RequestFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.Wrapped)]
string MemberSearch(string MemberName);
以下是我创建的RESTful服务
public string MemberSearch(string MemberName)
{
string MemberId = string.Empty;
string strSqlConn = string.Empty;
SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlCon"].ConnectionString);
sqlCon.Open();
SqlCommand sqlCom = new SqlCommand("Select MemberId From Member Where MemberName Like '%" + MemberName + "%'");
sqlCom.CommandType = System.Data.CommandType.Text;
sqlCom.Connection = sqlCon;
SqlDataReader rd = sqlCom.ExecuteReader();
while (rd.Read())
{
MemberId = Convert.ToString(rd["MemberId"]);
}
return MemberId;
}
以下是表中的数据
MemberId MemberName
1 John
2 Mike
3 Gracy
4 Smith
以下是AngularJS中的调用代码
var app = angular.module("app", []);
app.controller("main", function ($scope, $http) {
var MemberName = "Jo";
$scope.keyword = '';
$scope.GetAllData = function () {
$http.get('http://localhost/MemberEligibilityDemo/MemberService.svc/MemberSearch/' + MemberName)
.success(function (data, status, headers, config) {
$scope.Details = data;
})
.error(function (data, status, header, config) {
$scope.ResponseDetails = "Data: " + data +
"<br />status: " + status +
"<br />headers: " + jsonFilter(header) +
"<br />config: " + jsonFilter(config);
});
};
});
Angular代码不会成功或错误
URL如下
http://localhost/MemberEligibilityDemo/MemberService.svc/MemberSearch/
呼叫位置如下
http://localhost/Caller
WCF RESTful服务的结果如下
{"MemberSearchResult":"1"}
编辑
呼叫位置如下
<body ng-app="app">
<table id="tblContainer" ng-controller="main">
<tr>
<td><input type="button" name="DisplayDetails" value="Display Details" ng-click="GetAllData()"></td>
</tr>
<tr>
<td>
<p ng-bind="ResponseDetails"></p>
{{ResponseDetails}}
</td>
</tr>
</table>
为什么它没有在AngularJS代码中执行成功或错误。
答案
您应该将$http
函数更改为:
$http.get('http://localhost/MemberEligibilityDemo/MemberService.svc/MemberSearch/' + MemberName)
.then(function(resp) {
$scope.Details = resp.data;
}, function(err) {
var data = err.data;
var status = err.status;
var header = err.header;
var config = err.config;
$scope.ResponseDetails = "Data: " + data +
"<br />status: " + status +
"<br />headers: " + jsonFilter(header) +
"<br />config: " + jsonFilter(config);
});
我的回答是假设您有一个从UI调用此按钮或链接的按钮或链接。
我也猜测你正在迭代一个用户列表(ng-repeat
),并建议将你的方法更改为以下内容,并调用如下:GetAllData(memberId)
。
$scope.GetAllData = function (memberId) {
$http.get('http://localhost/MemberEligibilityDemo/MemberService.svc/MemberSearch/' + memberId)
// abbreviated
}
以上是关于$ http get不会在AngularJS中抛出成功或错误[重复]的主要内容,如果未能解决你的问题,请参考以下文章
如何解决“SparkException:Future.get 中抛出的异常”问题?
Spring Boot 示例在 Eclipse 中抛出 MojoExecutionException
主管不会在 econnrefused 上重新启动(在 init/1 中抛出)