$ http get不会在AngularJS

时间:2017-12-18 17:19:07

标签: angularjs wcf angularjs-1.6

以下是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代码不会成功或出错

网址如下

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代码中执行成功或错误。

1 个答案:

答案 0 :(得分:1)

您应该将$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);

    });
  

我的回答是假设您有一个从您的用户界面调用此按钮或链接。

我也猜测你正在迭代一个用户列表(ng-repeat),并建议将你的方法更改为以下内容,并调用如下:GetAllData(memberId)

$scope.GetAllData = function (memberId) {
    $http.get('http://localhost/MemberEligibilityDemo/MemberService.svc/MemberSearch/' + memberId)
    // abbreviated
}