2013-02-24 15 views
2

目前與我,當我嘗試連接到具有角JS我的REST的服務出現在我的Chrome開發者工具控制檯AngularJS:忽略Content-Type的參數

GET http://localhost:8080/movie_db/search/movie?movieTitle=Machete 415 (Unsupported Media Type) 

錯誤掙扎。由於我之前曾嘗試通過Chrome中的簡單REST插件訪問REST服務,所以我認爲最有可能缺少Content-Type標頭參數是原因(應爲Content-Type:'application/json 「)。

Angular JS網站上說,人們可以設置標題參數如下:$httpProvider.defaults.headers.get['My-Header']='value'。遺憾的是,這沒有任何影響,問題依然存在。

因此,我想知道如何操縱角JS的頭參數發送到我的REST服務。

就在那一刻,我的代碼看起來是這樣的:

function SearchMovieController($scope, $resource) { 

    $scope.SearchMovie = $resource('http://localhost\\:8080/movie_db/search/:action', 
      {action: 'movie', movieTitle: 'Machete'}, 
      {get: {method: 'JSONP', headers: [{'Content-Type': 'application/json'}, {'Accept' : 'application/json'}]}}); 

     $scope.SearchMovie.get() 
    } 

服務器側面看起來如下(我使用Spring MVC):

控制器:

@Controller 
@RequestMapping("/search/movie") 
public class SearchController { // TODO rename? 

private final TheMovieDBService theMovieDBService; 

@Autowired 
public SearchController(TheMovieDBService theMovieDBService) { 
    this.theMovieDBService = theMovieDBService; 
} 


@ResponseBody 
@RequestMapping(method = RequestMethod.GET, produces = "application/json", consumes = "application/json") 
public Map<String, Object> search(@RequestParam String movieTitle) { 
    Map<String, Object> response = new HashMap<String, Object>(); 

    try { 
     TheMovieDBSearchResult searchResult = theMovieDBService.searchMovie(movieTitle); 
     response.put("result", searchResult); 
     response.put("success", "true"); 
    } catch (EncoderException e) { 
     response.put("success", "false"); 
    } 
    return response; 
} 
} 

web.xml

<servlet> 
    <servlet-name>json</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 

    <init-param> 
     <param-name>contextClass</param-name> 
     <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> 
    </init-param> 

    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>com.u6f6o.apps.movie_db.config.web.ServletConfig</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>json</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 
+1

由於您使用JSONP我不認爲內容類型是相關的,除非服務器正在做一些奇怪的。 JSONP爲頁面添加了'