嗨,我有一个微服务在端口 8501 上运行。
@RestController
@RequestMapping("/feeds")
public class FeedsController {
@RequestMapping(method = GET)
ResponseEntity<?> findAllFeeds() {
return new ResponseEntity<String>("Hello", OK);
}
}
当我添加 url http://localhost:8501/feeds
时,浏览器显示“Hello”。现在我尝试通过 angularjs get call 访问它
在我的 AngularJs 中,我的代码是
'use strict';
var app = angular.module('commentsjsApp');
app.controller('MainCtrl', function ($scope,$http) {
$http.jsonp('http://localhost:8501/feeds').success(function(data, status, headers, config){
console.debug("Data : "+data+ "Status");
}).error(function(){
console.debug("error");
});
编辑: 在“网络”选项卡(firebug)中,我可以看到状态为 200 的 GET,响应为“Hello”。为什么我在控制台中收到错误?有哪位好心人可以帮助我吗?
当我运行这个 angularjs 应用程序时。控制台上的以下输出如图所示
最佳答案
您正在请求 JSONP 数据,但服务器返回一个字符串
。从服务器返回正确的 JSON 对象来解决问题。
注意:Hello world
不是有效的 JSON,但 "Hello World"
是。
关于javascript - Angularjs $http.get 方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20513702/