2013-02-24 48 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为页面添加了'