Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.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
春季及;AngularJS-与RESTful API的连接_Angularjs_Spring_Rest - Fatal编程技术网

春季及;AngularJS-与RESTful API的连接

春季及;AngularJS-与RESTful API的连接,angularjs,spring,rest,Angularjs,Spring,Rest,我的项目有一个奇怪的问题。我决定在Spring中编写服务器应用程序,并使用AngularJS编写前端。 在服务器端,我有一个简单的控制器,它从简单实体返回一些简单数据;)。看起来是这样的: @RestController public class ApiController { @RequestMapping( value = "/getAllProfiles", method = RequestMethod.GET) public Entity getEntity()

我的项目有一个奇怪的问题。我决定在Spring中编写服务器应用程序,并使用AngularJS编写前端。 在服务器端,我有一个简单的控制器,它从简单实体返回一些简单数据;)。看起来是这样的:

@RestController
public class ApiController
{
    @RequestMapping( value = "/getAllProfiles", method = RequestMethod.GET)
    public Entity getEntity()
    {
        Entity e = new Entity();
        e.setName( "myname" );
        return e;
    }
}
在正面,我有一个Angulars控制器,看起来像:

var app = angular.module('app', []);

app.config(function($httpProvider) {
    $httpProvider.defaults.useXDomain = true;   
    delete $httpProvider.defaults.headers.common['X-Requested-With'];
});

app.controller('Hello', function ($scope, $http) {
    $http.get({
        method: 'GET',
        url: 'http://localhost:8080/getAllProfiles'
    }).success(function (data) {
        console.log('success', data)
        $scope.greeting = data;
    }).error(function(){
        console.log("something goes wrong");
    });
})
和html文件:

<html ng-app="app">
<head>
    <title>Hello AngularJS</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.12/angular.min.js"></script>
    <script src="view1.js"></script>
</head>
<body>
<div ng-controller="Hello">
    <p>The content is {{greeting.name}}</p>
</div>
</body>
</html>

你好,安格拉斯
内容是{{greeting.name}

当我启动服务器时,我连接到localhost:8080/getAllProfiles,一切正常。我收到的JSON是这样的
{“name”:“myname”}
。但是,当我试着从角度做同样的事情时,我收到的内容是来自服务器的空数据,当然,错误函数正在控制器中调用。我认为这可能是CORS的问题,但我在服务器端添加了过滤器,在角度方面也没有帮助。 js控制台(我正在使用Chrome)显示:加载资源失败:服务器响应状态为404(未找到)

我真的不知道该怎么办。我想在这两个应用程序之间进行“对话”,但由于这个原因,我不能。
如果有人能帮我,我将非常感谢。

如果CORS在localhost上有问题,那么您可以使用。 在grunt文件中定义:

proxies: [
                {
                    context: '/',
                    host: '127.0.0.1',
                    port: 8080,
                    https: false,
                    xforward: false
                }
            ]
不要忘记将connext代理任务添加到任务中。 网站上的一切都解释得很清楚。 这将解决本地主机中的跨域问题。 然后,您可以在控制器中调用服务器:

$http.get({
        method: 'GET',
        url: '/getAllProfiles'
    })
这会起作用,因为grunt会在您的angular应用程序端口上运行代理

顺便说一下。 您是否尝试过通过单元测试来测试角度逻辑?
使用$httpbackend模拟与Spring应用程序完全相同的后端,您将看到结果。

好的,问题终于解决了。谢谢青色的帮助,咕噜声是个解决办法

我想为那些不想阅读本主题中所有内容的人描述此解决方案。;) 我想从某个角度的应用程序连接到我的SpringRESTAPI(两个独立的应用程序),但由于本地主机上的代理问题,我无法做到这一点。我已经为我的Angular应用程序安装了Grunt,配置了它,启动了服务器并试图从中获取数据。它掉下来了。 当然,服务器端需要CORS过滤器。它们在这里被描述

我使用了以下教程来帮助我:
还有来自青色的github链接

这可能没有什么区别,但我看到您在http.GET请求的配置中设置了GET的方法名,当然您不需要这一行,它已经是一个GETYes了,我知道。此http的结构之间的更改无法解决问题。如果您已检查CORS不是问题所在,您是否已再次检查a)spring应用程序正在8080上侦听,b)它没有绑定到的基本路径(即某些/foo/getAllProfiles)?是的,spring应用程序在8080上,因为当我在chrome-localhost:8080/getAllProfiles中这样做时,我从服务器得到了一个答案。从正面看是不行的。路径很好,你可以在上面的spring控制器中看到。不,我没有测试它。谢谢你,我会的。你能告诉我更多关于这个grunt连接的信息吗?我需要将它添加到我的angular应用程序中,并在一些配置中设置它?你使用grunt吗?它是grunt——一个为您编译项目的工具。我不知道是否有没有没有没有它的代理。我已经在我的项目中安装了Grunt。我不知道,也许我做错了什么,但我使用了这个教程和自述文件,你们在上面的帖子中给出的链接。它仍然不起作用。我将WebStrom用于我的前台应用程序,IntelliJ用于服务器应用程序。当我从WebStorm测试rest服务时,它工作正常(工具->测试RESTful服务)。我有Grunt,我有Gruntfile,一个问题没有解决。在启动Grunt期间,您是否有消息表明代理已启动?能否转到应用程序端口localhost:9000/getAllProfiles并查看数据?你用$httpBackend测试过吗?很有趣。我从命令行运行grunt serve,得到:“Running“serve”任务服务器正在端口9000上运行…”我在GrunFile文件中指定了代理:[{context:'/',host:'127.0.0.1',port:8080,https:false,xforward:false}],我不知道为什么它不使用代理…我有完全相同的问题,我已经为我的extJS应用程序安装了Grunt,并尝试在我的Spring MVC(&security)服务器应用程序上设置CORS过滤器,但它不起作用。如何检查服务器端的CORS过滤器?