0

enter image description hereI am creating a anjular js app where i have created a login page and i m authenticating it using web api. This is the controller which is returning json and it is running properly.

[Route("api/Customer/{id}/{pass}")]
public IEnumerable GetCustomer(string id, string pass)
{
        var list = repository.Get(id, pass);

        if (list != null)
        {
            return list;
        }
        else
        {
            return null;
        }
}

this is the controller accessing the api in angular app (demoApi.js)

(function () {
    'use strict';
    var DemoApi = function ($http) {
    var loginCheck = function (username, password) {
        return $http.get("http://localhost:8115/api/Customer/" +username+"/"+password)
            .then(function (response) {
                return response.data;
            });
        };
        return {
             loginCheck: loginCheck
        }
    };

    var app = angular.module("angularApp");
    app.factory("DemoApi", DemoApi);

}());

this is the login controller(loginController.js)

(function () {
var app = angular.module("angularApp");

var loginController = function ($scope, $http, bankApi, $location) {
    var userDetails = {
        username: $scope.uName,
        password: $scope.uPassword
    };

    $scope.login = function () {
        DemoApi.loginCheck(userDetails.username, userDetails.password)
            .then(onLogin, onError);
    };

    var onError = function (reason) {
        $scope.error = "Could not fetch the data.";

    };
    var onLogin = function (data) {
        $scope.details = data;
        $location.path("/info");
    };
}

app.controller('loginController', loginController);
}());

this is the index page where i have all the scripts linked

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="angularApp">
    <head>
   <title>Ebank</title>
   <script src="Scripts/angular.js"></script>
   <script src="Scripts/angular-route.js"></script>
   <link href="Content/bootstrap.min.css" rel="stylesheet" />
   <script src="lib/app.js"></script>
   <script src="lib/bankApi.js"></script>   
   <script src="lib/loginController.js"></script>
   <script src="lib/infoController.js"></script>
   </head>
   <body style="background-color:#333">
   <div ng-view></div>
   </body>
</html>

Provide me with a solution, why i m not able to call api from demoApi.js I'm getting the response if i call this api directly from the html page but not getting response from demoApi.js

abhishek
  • 301
  • 1
  • 5
  • 29
  • Please see http://stackoverflow.com/questions/19093603/simple-post-to-web-api .. you are missing `[HttpPost]` attribute over the controller method – Amitd Dec 03 '16 at 18:00
  • Still it is not working..@Amitd – abhishek Dec 05 '16 at 06:31
  • the get customer is returning me an array or say json object but how should i catch the response data in angular App – abhishek Dec 05 '16 at 06:36
  • please post screenshot of the error, Your onLogin should be called if the call completes successfully . Check your onError ..the reason parameter will have more details of the error. – Amitd Dec 05 '16 at 15:22
  • I have provided the screenshot @Amitd – abhishek Dec 06 '16 at 06:12
  • Can you check this answer .. http://stackoverflow.com/questions/23293782/mvc-web-api-405-method-not-allowed – Amitd Dec 06 '16 at 07:11

1 Answers1

0

Most probably you bumped into CORS issue. You need to enable CORS. Follow below mentioned steps.

  1. Open IISExpress config file applicationhost.config located at C:\Program Files (x86)\IIS Express\AppServer.
  2. Search for "httpProtocol" tag. Inside it check if "customHeaders" tag exists. If not then add it.
  3. Add following two tag inside "customHeaders" tag.

<add name="Access-Control-Allow-Origin" value="*" /> <add name="Access-Control-Allow-Headers" value="Content-Type" />

Alternatively you can run chorme browser in security disabled mode using following command on command prompt and try accessing your site from there.

Full path to chrome.exe --disable-web-security --user-data-dir

Note its just workaround to test your application however in production deployment you need to enable CORS.

Pankaj Kapare
  • 7,486
  • 5
  • 40
  • 56