简体   繁体   中英

ASP.NET MVC WebAPI Not Returning Data

I'm trying to call a Web API from my website using Angular JS but I'm not getting any data back.

If I run the API separately from my website I get a response back so I know the service is running okay and returning data so it must be the way I'm calling or trying to display the data from my API.

Below is the code from my JS controller on my website.

Any ideas about what the problem is? Thanks

(function () {
var MealsController = function ($scope, $http) {
    var meals = function (serviceResp) {
        $scope.Meals = serviceResp.data;
    };
    var errorDetails = function (serviceResp) {
        $scope.Error = "Something went wrong ??";
    };
    $http.get("http://localhost:2153/api/meal")
        .then(meals, errorDetails);
    $scope.Title = "Meals Details Page";
    $scope.Meals = meals;
};
app.controller("MealsController", MealsController); }());

And here is my view HTML code

<!DOCTYPE html>
<html lang="en" ng-app="MealPlannerModule">
<head>
<title>Employee Details</title>
<script src="Scripts/jquery-2.1.3.min.js"></script>
<link href="CSS/amelia.bootstrap.min.css" rel="stylesheet" />
<link href="CSS/Site.css" rel="stylesheet" />
<script src="Scripts/bootstrap.min.js"></script>
<script src="Scripts/angular.min.js"></script>
<script src="Scripts/app.js"></script>
<script src="Controllers/MealsController.js"></script>
</head>
<body ng-controller="MealsController">
<p>Meal Name - {{Meals.mealID}}</p>
</body>
</html>

I suppose that you are violating the same origin policy by making a cross domain AJAX request here: http://localhost:2153/api/meal . Make sure you have enabled CORS on your Web API for this to work.

Also never forget to look at the console tab in your web browser which will contain useful information about why your code didn't work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM