无法使用html5和AngularJS从WebApi服务获取数据 [英] Cannot GET data from WebApi service, using html5 and AngularJS
问题描述
大家好。
我刚开始发现网络技术的世界。所以我用AngularJS尝试了html5。它非常好,尤其是数据绑定,遗憾的是我无法通过如何获得$ http.get方法的误解。我尝试了很多种形式的调用,但是我无法从WebApi服务中获取任何数据。这是我的客户端,html代码和js:
Hello everyone
I just have started to discover the world of web technologies. So I tried html5 with AngularJS. It is very nice, particularly the data binding, unfortunately I cannot get trough how to get the mistery of $http.get method. I tried many forms of calling it, but I could not get any data from my WebApi service. Here is my client side, the html code, and the js:
<!DOCTYPE html>
<html ng-app="MyApp">
<head>
<link rel="stylesheet" href="css/bootstrap.min.css">
<script src="http://buas011/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module("MyApp", []);
app.controller("HelloController", function ($scope, $http) {
$scope.getData = function () {
$scope.text = "clicked";
try {
$http({ method: 'GET', url: 'http://localhost:53159/api/search', params: { id: '5' }
}).success(function (data, status, headers, config) {
$scope.text = "Data!";
}).
error(function (data, status, headers, config) {
$scope.text = "Unsuccess " + data + " " + status;
});
} catch (e) {
$scope.text = "Errorr! " + e;
}
}
})
</script>
</head>
<body ng-controller="HelloController">
<p>Hello {{text}}</p>
<input type="text" ng-model="text" >
<form ng-submit="getData()" >
<input type="submit" value="Get data" class="btn btn-info">
</form>
</html>
在webapi方面,我使用此代码:
On the webapi side I use this code:
public class SearchController : ApiController
{
// GET: /Search/id
public string Get(int id)
{
// can be userid
return "userid: " + id+1;
}
}
如果我在webbrowser中使用链接进行GET,它可以正常工作(http:// localhost:53159 / api / search / 5)但是从网站上我总是运行错误方法。
感谢您阅读本文,并提前致谢!
问候Adam!
If I use link in webbrowser for the GET, it works (http://localhost:53159/api/search/5" ) but from the website I always run to the error method.
Thanks for reading this, and thanks in advance!
Greetings Adam!
推荐答案
http.get方法。我尝试了很多形式的调用它,但我无法从我的WebApi服务中获取任何数据。这是我的客户端, html代码和js:
http.get method. I tried many forms of calling it, but I could not get any data from my WebApi service. Here is my client side, the html code, and the js:
<!DOCTYPE html>
<html ng-app="MyApp">
<head>
<link rel="stylesheet" href="css/bootstrap.min.css">
<script src="http://buas011/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module("MyApp", []);
app.controller("HelloController", function (
scope,
http){
这篇关于无法使用html5和AngularJS从WebApi服务获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!