I have encountered a similar issue for my all example programs with angularJS fetch data from the WebApi. I think there is nothing to do with my code, but some settings that may be I ignored, please help me check it: here is my web api: URL:http://localhost:1883/api/Subcriber and return the data:

enter image description here

Html body:

<div id="tblSubs" ng-controller="APIController">
    <table>
        <tr>
            <th>ID</th>
            <th>Email ID ( Double click to update)</th>
            <th>Subscribed Date</th>
            <th></th>
        </tr>
        <tbody data-ng-repeat="sub in subscriber">
            <tr>
                <td>{{sub.SubscriberID}}</td>
                <td ng-blur="updSubscriber(sub,$event)" ng-dblclick="makeEditable($event)">{{sub.MailID}}</td>
                <td>{{sub.SubscribedDate}}</td>
                <td>
                    <input type="button" id="Delete" value="Delete" data-ng-click="dltSubscriber(sub.SubscriberID)" />
                </td>
            </tr>
        </tbody>
    </table>
    <div class="form-group">
        <label for="email">Sbscribe here</label>
        <input type="email" class="form-control" id="email" placeholder="Enter email" [required="string" ] data-ng-model="mailid" />
    </div>
    <button type="button" class="btn btn-default" data-ng-click="saveSubs();">Submit</button>
</div>
<script src="Scripts/angular.js"></script>
<script src="Scripts/angular-route.js"></script>
<script src="Scripts/ApiScripers/Module.js"></script>
<script src="Scripts/ApiScripers/Service.js"></script>
<script src="Scripts/ApiScripers/controller.js"></script>

AngularJS Module:

    var app;
(function () {
app=angular.module('APIModule', []);
})();

AngularJS Service:

app.service("APIService", function ($http) {
this.getSubs = function () {
    var url = 'api/Subscriber';
    return $http.get(url).then(function (response) {
        return response.data;
    });
}

AngularJS Controller:

        app.controller('APIController', function ($scope, APIService) {
getAll();
function getAll() {
    var servCall = APIService.getSubs();
    servCall.then(function (d) {
        $scope.subscriber = d.data;
    }, function (error) {
        $log.error('Oops! Something went wrong while fetching the data.')
    })
}   
})

After running, there is no error information from browser debug window. It is too strange, whether I need to publish my webapi to IIS and then call it?

Update: Index.cshtml:

@{
    ViewBag.Title = "Welcome";
}
<style>
    table, tr, td, th {
        border: 1px solid #ccc;
        padding: 10px;
        margin: 10px;
    }
</style>

<h2>Welcome to Sibeesh Passion's Email List</h2>
<body data-ng-app="APIModule">
    <div id="tblSubs" ng-controller="APIController">
        <table>
            <tr>
                <th>ID</th>
                <th>Email ID ( Double click to update)</th>
                <th>Subscribed Date</th>
                <th></th>
            </tr>
            <tbody data-ng-repeat="sub in subscriber">
                <tr>
                    <td>{{sub.SubscriberID}}</td>
                    <td ng-blur="updSubscriber(sub,$event)" ng-dblclick="makeEditable($event)">{{sub.MailID}}</td>
                    <td>{{sub.SubscribedDate}}</td>
                    <td>
                        <input type="button" id="Delete" value="Delete" data-ng-click="dltSubscriber(sub.SubscriberID)" />
                    </td>
                </tr>
            </tbody>
        </table>
        <div class="form-group">
            <label for="email">Sbscribe here</label>
            <input type="email" class="form-control" id="email" placeholder="Enter email" [required="string" ] data-ng-model="mailid" />
        </div>
        <button type="button" class="btn btn-default" data-ng-click="saveSubs();">Submit</button>
    </div>
</body>
<script src="~/Scripts/angular.js"></script>
<script src="~/Scripts/angular-route.js"></script>
<script src="~/Scripts/APIScripts/Module.js"></script>
<script src="~/Scripts/APIScripts/Service.js"></script>
<script src="~/Scripts/APIScripts/Controller.js"></script>

_Layout.cshtml:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>
<body>
    <div class="navbar navbar-inverse navbar-fixed-top">
    </div>
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>&copy; @DateTime.Now.Year - Sibeesh Passion</p>
        </footer>
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>

.cshtml could fetch the data from the webapi, but I just copy the html code to a .html file that I could not access the data. Hope someone could help it. There is nothing wrong with the code, may be I ignored some includes.

share|improve this question
    
It is too strange that I have copied the source code from the .cshtml file to html file. And the issue appeared again. here is my project: – qianli Mar 6 at 15:09
    
Questions: 1. Is there a network call visible in network tab and does it have response? 2. By putting a console.log in service, you must find if service is being call and returns data correctly. And finally: 3. Is the value available in variable d in controller. This gives a narrowed down scope of the problem. If you have energy left, reproduce the problem on jsfiddle. – Priya Ranjan Singh Mar 6 at 15:10
    
post your webapi code also – federico scamuzzi Mar 6 at 15:11
    
I will share my project, here is my project: 1drv.ms/f/s!AiYjxW6ElFsyvkUdoh3T1o3CiW5g – qianli Mar 6 at 15:14
    
HtmlPage1.html is run problems, but the code are the same as index.cshtml file, this is my surprise. This project could be run without any problems, I just modified the datasource, and I get it from here: gallery.technet.microsoft.com/Web-API-With-Angular-JS-8f4ac0‌​3d – qianli Mar 6 at 15:16

Be sure to throw the error to see it in the console window:

function getAll() {
    var servCall = APIService.getSubs();
    servCall.then(function (d) {
        $scope.subscriber = d.data;
    }, function (error) {
        $log.error('Oops! Something went wrong while fetching the data.')
        //THROW error
        throw error;
    })
}

When a promise .then method rejection handler omits a throw statement, the handling function returns a value of undefined. This will convert a rejected promise to a fulfilled promise.

$q:

Due to e13eea, an error thrown from a promise's onFulfilled or onRejection handlers is treated exactly the same as a regular rejection. Previously, it would also be passed to the $exceptionHandler() (in addition to rejecting the promise with the error as reason).

-- AngularJS Developer Guide - Migrating from V1.5 to V1.6.

share|improve this answer
    
I think there is noting to do with my code, I just copy the html code from the cshtml file. And nothing changed. so I doubt that whether when I run the angularjs whether need other js library such as jquery, or others. I have imported the JQuery library. This is amazing me. Thank you very much. – qianli Mar 7 at 7:38
up vote 0 down vote accepted

It is working, I need to import some necessary libraries, such as jquery and bootstrap.js. But I found another question that: At first, I think my code do not work, may be I was wrong, the speed of fetching data from the webapi is too delay, so I do not see it any more. Just 2s later, the data are all displayed successfully. So the speed of .cshtml file is faster than .html file, what reason caused this issue. Does some one have the same issue as mine.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.