转载Request method 'OPTIONS' not supported

1down votefavorite

I am using Spring Boot in the server side and Angularjs in client side, i configured Spring Bootapplication CORS using a Filter and it worked for GETPOST methods, but, when i tried to send PUTrequest using $http module, i got this in my browser console :

Notice: url = http://localhost:8080/localbusinessusers/[object%20Object]

OPTIONS url (anonymous function) @ angular.js:11442sendReq @ angular.js:11235serverRequest @ angular.js:10945processQueue @ angular.js:15552(anonymous function) @ angular.js:15568Scope.$eval @ angular.js:16820Scope.$digest @ angular.js:16636Scope.$apply @ angular.js:16928done @ angular.js:11266completeRequest @ angular.js:11464requestLoaded @ angular.js:11405 app.html:1 XMLHttpRequest cannot load url. Response for preflight has invalid HTTP status code 405 angular.js:11442 XHR failed loading: PUT "url".

And i got this on Spring Boot console:

2016-03-08 23:19:51.212 WARN 27044 --- [ XNIO-2 task-2] o.s.web.servlet.PageNotFound : Request method 'OPTIONS' not supported

My CORS Filter:

package com.datcom.fouras;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;

@Component
public class CorsFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // TODO Auto-generated method stub

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
            throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin", "http://localhost");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type, x-requested-with, X-Custom-Header");
        chain.doFilter(req, res);

    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

}

My REST method in my RestController:

@RequestMapping(value = "/localbusinessusers", method = RequestMethod.PUT)
    public LocalBusinessUser update(LocalBusinessUser localBusinessUser) {
        log.debug("REST request to update a local business user : {}", localBusinessUser);
        if (localBusinessUser.getId() == null) {
            return save(localBusinessUser);
        } else {
            LocalBusinessUser localBusinessUser2 = localBusinessUserRepository.findOne(localBusinessUser.getId());
            localBusinessUser2 = localBusinessUser;
            return localBusinessUserRepository.saveAndFlush(localBusinessUser2);
        }
    }

And finally, my $http request:

 

$http.put('http://localhost:8080/localbusinessusers/' + localBusinessUser)
                .success(function (data, status, headers) {
                    console.log('data: ' + data);
                })
                .error(function (data, status, header, config) {
                    console.log('data: ' + data);
                    console.log('status: ' + status);
                    console.log('header: ' + header);
                    console.log('config: ' + config);
                });

 

Notice: PUT request is working using Advanced REST Client

javascript angularjs spring spring-boot cors

shareimprove this question

asked Mar 8 at 22:50

Jemli Fathi

39110

 

add a comment

 

1 Answer

activeoldestvotes

 

up vote0down voteaccepted

If Options requests are being prohibited, you can configure the DispatcherServlet to dispatch all options requests by adding this to your application.properties:

spring.mvc.dispatch-options-request=true

For more information about options requests and Spring MVC using Spring Boot, there is a nice post for Handling Options Request using Spring Boot

You can also try the examples in this post for handling CORS and supporting Options requests by creating a simple controller to return NO_CONTENT for all Options requests:http://dontpanic.42.nl/2015/04/cors-with-spring-mvc.html

shareimprove this answer

edited Mar 8 at 23:36

answered Mar 8 at 23:16

pczeus

4,16111027

 
   

i edited my application.properties file but i still get the same errors – Jemli Fathi Mar 8 at 23:23

   

I updated the answer with an additional link. – pczeus Mar 8 at 23:37

   

Did the NO_CONTENT options request controller help you? – pczeus Mar 10 at 13:25

   

Actually, i added these lines to application.properties file: # ENDPOINTS CORS CONFIGURATION (EndpointCorsProperties) endpoints.cors.allow-credentials = false endpoints.cors.allowed-headers = Content-Type, x-requested-with, X-Custom-Header endpoints.cors.allowed-methods = * endpoints.cors.allowed-origins = 164.132.198.236localhost endpoints.cors.max-age = 3600 – Jemli Fathi Mar 10 at 13:31 

转载于:https://my.oschina.net/yizhichao/blog/718639

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值