$ http get不会在AngularJS中抛出成功或错误[重复]

问题描述 投票:0回答:1

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

angularjs wcf angularjs-1.6
1个回答
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);

    });

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

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

$scope.GetAllData = function (memberId) {
    $http.get('http://localhost/MemberEligibilityDemo/MemberService.svc/MemberSearch/' + memberId)
    // abbreviated
}
© www.soinside.com 2019 - 2024. All rights reserved.