2017-03-17 63 views
1

我有一個彈簧RestController,這是我使用SpringBoot開發的。

@RestController 
@RequestMapping("/myRoot") 
public class MyRestController { 
    @Autowired 
    private MyService myService; 

    @RequestMapping(value="/add/{myItem}", method=RequestMethod.POST) 
    public @ResponseBody void addMyItem(@PathVariable("myItem") String myItem){ 
     myService.addMyItem(myItem); 
    } 
} 

main方法我的春天引導類是:

@SpringBootApplication(scanBasePackages={"org.mypackage"}) 
public class MyRestApp { 
public static void main(String[] args) { 
    SpringApplication.run(MyRestApp .class, args); 
} 
} 

當我編譯該項目運行生成的JAR在Eclipse的Java應用程序,控制檯顯示:

2017-03-17 22:33:14.363 INFO 2292 --- [   main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) 
2017-03-17 22:33:14.371 INFO 2292 --- [   main] o.n.todo.springboot.SuperrDuperrRestApp : Started MyRestApp in 7.056 seconds (JVM running for 7.837) 

製作在郵政局Chrome上的http://localhost:8080/MyRestApi/myRoot/add/myItem郵寄回電成功(200 OK)。

這意味着我的其餘api已正確顯示並正常運行。

我想上面休息URI調用從我AngularJs如下1個應用:

'use strict'; 

var myApp = angular.module("MyFirstAngularRestApp",[]); 
myApp.controller("myCtrl",myCtrl); 

function myCtrl($http){ 
    console.log("myCtrl"); 

    var REST_SERVICE_URI = 'http://localhost:8080/MyRestApi/'; 

    this.AddItem = function(newItem){ 
    console.log("AddItem"); 
    console.log(newItem); 

    $http.post(this.REST_SERVICE_URI + 'myRoot/add/' + newItem).then(function(response){ 
     console.log("success"); 
    }); 
    } 
} 

但是,這是行不通的。我收到以下錯誤:

XMLHttpRequest cannot load file:///C:/myDevelopment/Angular/MyAngularApp/undefinedtoDo/add/ReadABook. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource. 
Possibly unhandled rejection: {"data":null,"status":-1,"config":{"method":"POST","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"undefinedtoDo/addItem/Read a Book","headers":{"Accept":"application/json, text/plain, */*"}},"statusText":""} 

我絕對新AngularJs 1,所以無法理解爲什麼我的REST調用是行不通的。我錯過了什麼?

我的AngularJs項目位於筆記本電腦驅動器的不同位置,而我的eclipse工作區是不同的目錄。但我相信這不應該是重要的。

我會很感激,如果有人能指導我如何解決這個錯誤。請儘可能詳細地回答,因爲我是新手。

謝謝!

回答

1

這是一個交叉錯誤,嘗試用@CrossOrigin註釋您的控制器,它將允許您的應用程序調用API。使用註釋,您可以指定域(原點)和方法(PUT,GET,POST等)。查看更多:http://docs.spring.io/spring-framework/docs/4.2.4.RELEASE/javadoc-api/org/springframework/web/bind/annotation/CrossOrigin.html

如果要定義要允許域,使用下面的代碼在配置類。在這種情況下,你不需要註釋所有的控制器:

@Bean 
public WebMvcConfigurer corsConfigurer() { 
    return new WebMvcConfigurerAdapter() { 
     @Override 
     public void addCorsMappings(CorsRegistry registry) { 
      registry.addMapping("/**/*").allowedOrigins("locahost:8080"); 
     } 
    }; 
} 
0

你的角度應用程序和服務器在不同的起源和瀏覽器中運行一般,不支持,由於same-origin policy

您可以輕鬆地通過註解你的控制器或特定的方法解決這個問題,如果你想與@CrossOrigin註解如下:

@CrossOrigin(origins = "http://localhost:4200", methods = { RequestMethod.POST, RequestMethod.GET, RequestMethod.DELETE, 
    RequestMethod.PUT }) 

正如你所看到的,你可以用特定的主機或支持的http方法等。