Javascript 无法使用AngularJS解析JSON
我刚开始学习AngularJS,并尝试了一个非常基本的示例。该网页包含一个按钮,单击该按钮,我会从服务器请求一个json。 但每当我单击按钮时,代码就会执行error函数中的部分 HTML文件Javascript 无法使用AngularJS解析JSON,javascript,html,json,angularjs,Javascript,Html,Json,Angularjs,我刚开始学习AngularJS,并尝试了一个非常基本的示例。该网页包含一个按钮,单击该按钮,我会从服务器请求一个json。 但每当我单击按钮时,代码就会执行error函数中的部分 HTML文件 <!DOCTYPE html> <head> <title> step 4</title> </head> <body ng-app="step4App"> <div ng-controller="Friend
<!DOCTYPE html>
<head>
<title> step 4</title>
</head>
<body ng-app="step4App">
<div ng-controller="FriendsCtrl">
<button ng-click="loadFriends()">Load Friends</button>
</div>
<script src="angular.min.js"></script>
<script src="xml2json.js"></script>
<script src = "step4.js"></script>
</body>
</html>
但我不明白,为什么它总是执行错误函数
我在浏览器控制台中检查了是否正在发出请求并收到47字节的响应(等于JSON中的字符数)。但是它不会打印JSON。服务器上可能发生了错误。尝试使用FireFox+Firebug打开页面,并检查服务器响应以确保其符合预期(检查状态代码和内容类型响应标题) 您还可以从命令行运行doa
curl-vhttp://localhost:8080/test.jsp
顺便说一句,Angular在回调中传递额外的参数。试着提醒他们,看看他们是什么。从
谢谢我试着用firebug。它表示状态为200(OK),但angularJS表示为404。您可能想检查JSP文件中的逻辑,可能有问题。或者确保你的URL没有输入错误。
var app = angular.module("step4App",[]);
app.controller("FriendsCtrl", function($scope,$http) {
$scope.loadFriends = function(){
$http({method:'GET',url:'http://localhost:8080/test.jsp', params:{name:"abc",no:"LBF1151638"}}).success(function(data) {
$scope.friends = data;
document.write('<p>two</p>');
document.write(data);
}).error(function() {
alert("error");
});
}
});
{"response":{"numFound":0,"start":0,"docs":[]}}
$http({method: 'GET', url: '/someUrl'}).
success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
}).
error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});