Javascript 为什么有棱角';s$http.get服务不工作?
我在Angular services上找到的一个示例遇到了问题,在这个特定的案例中$http.get one 我有一个json.php文件,其中有下一个json:Javascript 为什么有棱角';s$http.get服务不工作?,javascript,php,html,angularjs,Javascript,Php,Html,Angularjs,我在Angular services上找到的一个示例遇到了问题,在这个特定的案例中$http.get one 我有一个json.php文件,其中有下一个json: { "bands": [ { "name": "Red hot chilli peppers", "year": "2009", }, { "name": "Maroon 5", "year": "2005", }, { "name
{
"bands": [
{
"name": "Red hot chilli peppers",
"year": "2009",
},
{
"name": "Maroon 5",
"year": "2005",
},
{
"name": "ACDC",
"year": "2000",
}
]
}
Angular和HTML代码应该在对象上迭代并显示其中的数据,但它不起作用:
JavaScript
var serviceTestApp = angular.module('myapp3', []);
serviceTestApp.controller('controller3', function($scope, $http){
$http.get('json.php').then(function(response){
$scope.myData = response.data.bands;
});
});
HTML
<div ng-app='myapp3' ng-controller='controller3'>
<ul>
<li ng-repeat='x in myData'>
{{x.name}}
</li>
</ul>
</div>
-
{{x.name}
我做错了什么?我用Angular 1.5.0当你的意思是不工作时,你到底是什么意思?你的意思是它成功了,但没有返回你想要的吗?在这种情况下,您可能指向对象中的错误对象。 或者它会抛出一个错误 您应该这样构造它:
$http.get('json.php').then(function(response){
// Place a debugger statement here to see if it's successful
$scope.myData = response.data.bands;
}, function(error) {
// No breakpoint needed as you'll see the error in the console window
console.error(error);
});
另一个原因可能是您没有将文件放在正确的位置
json.php
意味着我希望它与控制器位于同一文件夹中。console.log(response.data.bands);控制台它的输出是什么?返回了什么错误?处理你的错误<代码>$http.get('json.php')。然后(函数(响应){$scope.myData=response.data.bands;},函数(错误){//console.log(错误);)请放置一个可访问的json.php链接,因为jsut json.php在这里不起作用我无法调试您的问题您可以从php返回json以便进行http调用,或者检查控制台并粘贴错误吗