0

The below is the declaration of WCF RESTful service for GET

[OperationContract]
[WebGet(UriTemplate= "MemberSearch/{MemberName}", ResponseFormat=WebMessageFormat.Json,RequestFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.Wrapped)]
string MemberSearch(string MemberName);

The below is the RESTful Service I have created

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;
    }

The below is the data in the table

MemberId MemberName
1   John        
2   Mike        
3   Gracy       
4   Smith       

The below is the calling code in 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);
        });
};
});

The Angular code does not either come into success or error for this

The URLs are as below

http://localhost/MemberEligibilityDemo/MemberService.svc/MemberSearch/

Calling location is as follows

http://localhost/Caller

The result from WCF RESTful service is as below

{"MemberSearchResult":"1"}

EDIT

The calling location is as below

<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>

Why it is not executing either success or error in AngularJS code.

Brian
  • 4,921
  • 3
  • 20
  • 29
CPK_2011
  • 872
  • 3
  • 21
  • 57
  • There is no evidence of where you ever call `$scope.GetAllData()`. Could you add that? We also can't see evidence that the URL path would be appropriate here from the WCF side. – Brian Dec 18 '17 at 17:21
  • 2
    Also, AngularJS version? `.success()` isn't valid as of 1.6: https://stackoverflow.com/questions/35329384/why-are-angular-http-success-error-methods-deprecated-removed-from-v1-6/35331339#35331339 – Brian Dec 18 '17 at 17:24
  • Your code is susceptible to SQL injection attacks. Please look up how to use parameterized queries. – Nikolaj Dam Larsen Dec 18 '17 at 17:24
  • @Brian Yes. I am using AngularJS version 1.6.5 – CPK_2011 Dec 18 '17 at 17:43

1 Answers1

1

You should change your $http function to this:

$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);

    });

My answer assumes that you have a button or link that is calling this from your UI.

I am also guessing that you are iterating over a list of users (ng-repeat), and would suggest changing your method to what is below, and calling like this: GetAllData(memberId).

$scope.GetAllData = function (memberId) {
    $http.get('http://localhost/MemberEligibilityDemo/MemberService.svc/MemberSearch/' + memberId)
    // abbreviated
}
georgeawg
  • 48,608
  • 13
  • 72
  • 95
Brian
  • 4,921
  • 3
  • 20
  • 29
  • With the above answer, I am getting `then` executed. It is returning object and how can I deserialize it? – CPK_2011 Dec 18 '17 at 17:58
  • That depends on what is coming back..it should be a JSON object. If it has a property `name`, it would be `resp.name`, etc.. – Brian Dec 18 '17 at 18:01