我对 AngularJS 非常陌生,我正在尝试从我使用 ServiceStack 快速创建的 Web 服务中获取 JSON 中的一些项目。当我在浏览器中尝试该 URL 时,我可以看到 JSON 对象,但由于某种原因 AngularJS 无法成功获取数据。这是我的实现:
angular.module('SomeApp', []).controller('ItemsFetcher', [ '$http', function($http){
var x = this;
x.products= [
{
Name: 'Missing items',
Description: 'Could not access server'
}];
$http.get('http://localhost:29029/json/reply/GetAllItemsRequest')
.then(function(data){
x.products = [
{
Name: 'Success',
Description: 'Great stuff!'
}];
});
}]);
这是 View :
<html ng-app="SomeApp">
<head>
<title>My First App</title>
<script type="text/javascript" src="angular.min.js"></script>
<script type="text/javascript" src="app.js"></script>
</head>
<body ng-controller="ItemsFetcher as fetcher">
<h3>PRODUCTS</h3>
<div ng-repeat="item in fetcher.products">
<div>{{item.Name}} - {{item.Description}}</div>
</div>
</body>
正如我所说,如果我调用 http://localhost:29029/json/reply/GetAllItemsRequest在浏览器中,我可以获取 JSON 对象。
我错过了什么吗?有什么想法为什么这行不通吗?
最佳答案
如果任何人都可以从中受益,我必须启用 CORS ( http://en.wikipedia.org/wiki/Cross-origin_resource_sharing ) 或将网络服务和网站托管在同一位置。
关于javascript - get ('url' ) 使用 AngularJS 和 ServiceStack webservice 进行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28617921/