Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Angular$http响应在控制台中打印时返回null,但请求成功_Javascript_Angularjs_Json - Fatal编程技术网

Javascript Angular$http响应在控制台中打印时返回null,但请求成功

Javascript Angular$http响应在控制台中打印时返回null,但请求成功,javascript,angularjs,json,Javascript,Angularjs,Json,我有一个用Spring开发的本地Web服务,一切正常(使用浏览器或Postman),但是当我尝试使用Angular/Ionic的简单GET方法时,它在响应中返回null 但当我查看Firebox控制台上的Net选项卡时,我看到请求已成功发出,并返回了预期将返回的真正JSON $http.get('http://localhost:8081/jungery/card/getCard/1').success(function(response) { console.log('Response:'

我有一个用Spring开发的本地Web服务,一切正常(使用浏览器或Postman),但是当我尝试使用Angular/Ionic的简单GET方法时,它在响应中返回null

但当我查看Firebox控制台上的Net选项卡时,我看到请求已成功发出,并返回了预期将返回的真正JSON

$http.get('http://localhost:8081/jungery/card/getCard/1').success(function(response) {
  console.log('Response:', response); // response is null
}).error(function(data, status, headers, config) {
  console.log('Error:', data);
});
Firefox上的网络选项卡:

在服务器端调试时,我可以确认webservice上的方法确实被调用了

@RequestMapping(value = "/getCard/{id}", method = { RequestMethod.GET, RequestMethod.OPTIONS }, headers = "Accept=application/json")
public Card getCardById(@PathVariable int id) {
    Card item = cardService.getCard(id);
    if (item == null) {
        item = new Card();
        item.setId(0L);
    }
    return item;
}

所以,我的问题是:我在这里遗漏了什么?我以前做过,从未有过这样奇怪的行为。

如果你确保你的Web服务正常工作,那可能是客户的错。 尝试“然后”而不是“成功”

您可以在此处阅读更多为什么优先使用此方法:
此外,当使用AngularJS的REST服务时,您可以考虑使用Resturn:

<代码>成功< /代码>和<代码>错误>代码>(参见$HTTP DOCs)…尝试使用<代码>())/代码>我如您所建议的,更改了结果,但更改不好,现在显示:09:09:00.147跨源请求被阻止:同源策略不允许读取“”处的远程资源。(原因:如果CORS标头“Access Control Allow Origin”为“*”,则不支持凭据)。1(未知),响应打印为对象,但数据为null:response:object{data:null,status:-1,headers:headersGetter/如果使用firefox,此插件可能会有所帮助。
$http.get('http://localhost:8081/jungery/card/getCard/1').then(function(response) {
    console.log('Response:', response);
}, function(data, status, headers, config) {
    console.log('Error:', data);
});