AngularJS REST调用到另一台服务器 [英] AngularJS REST call to another server

查看:134
本文介绍了AngularJS REST调用到另一台服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是约曼发电机AngularJS 的,我愿与互动一个REST服务器(谁使用Spring引导)。

I'm using the Yeoman generator for AngularJS and I would like to interact with a REST server (who uses Spring Boot).

在我的电脑:

  • AngularJS app starts with Grunt on http://localhost:9000/
  • Spring Boot app is on http://localhost:8080/

在我AngularJS应用程序我有这样的服务:

In my AngularJS app I have a service like that :

angular.module('myApp')
  .factory('User', ['$resource',
    function($resource){
      return $resource('http://localhost:8080/poc/hello', {}, {
        query: {method:'GET'}
      });
  }]);

而这里是控制器:

And here is the controller :

angular.module('myApp')
  .controller('RegisterCtrl', ['$scope', 'User', function($scope, User) {
      $scope.hello = User.query();
}]);

在我的Java应用程序:

In my Java app :

@RestController("poc")
public class PocController {

    @RequestMapping("hello")
    public String getHello() {
        return "Hello World !";
    }
}

但是,当我想要做的号召,我被封锁CORS...

But when I want to do the call, I'm being blocked by "CORS"...

什么是做我想做的事情的最好方法?不仅为GET请求,但POST,等等...

What is the best way to do what I want to do ? Not only for GET request, but POST, etc...

是否有可能配置的REST服务器URL(的http://本地主机:8080 / )一次,让所有AngularJS电话(如 / POC /你好)重定向到 HTTP ://本地主机:8080 / POC /你好?如果是的话,怎么样?

Is it possible to "configure" the REST server URL (http://localhost:8080/) once and make all AngularJS call (like /poc/hello) redirected to http://localhost:8080/poc/hello ? If yes, how ?

推荐答案

在您休息的服务器设置头,下面是一些数据我我的节点服务器上设置,但细节应该有所帮助:

set headers in your rest server, following is some data I set on my node server but the details should help:

'Access-Control-Allow-Origin': 'http://localhost:9000'
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS, PUT, PATCH, DELETE'
'Access-Control-Allow-Headers': 'X-Requested-With,content-type'
'Access-Control-Allow-Credentials': false

这篇关于AngularJS REST调用到另一台服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆