vue axios springmvc 跨域传参

欧阳小白 6月前 ⋅ 174 阅读

No 'Access-Control-Allow-Origin' header is present on the requested resource.'Ajax跨域访问解决方案 The Response had HTTP code 403.
前台配置

vue在配置文件中提供了proxyTable来设置跨域,在config文件夹的index.js文件中

proxyTable: {
  '/api': {
    target: 'http://192.168.5.103:8081/', // 接口的域名
    // secure: false,  // 如果是https接口,需要配置这个参数
    changeOrigin: true, // 如果接口跨域,需要进行这个参数配置
    pathRewrite: {
      '^/api': ''
    }
  }
},

dev.env.js:生产环境

API_HOST:"/api/"

prod.env.js:正式环境

API_HOST:'"http:/192.168.0.105:8081/"'

后台配置(创建一个新的java类)

package linkjb.base;
 
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class ProcessInterceptor implements HandlerInterceptor {
 
 
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
 
        httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
 
        httpServletResponse.setHeader("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
 
        httpServletResponse.setHeader("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
 
        httpServletResponse.setHeader("X-Powered-By","Jetty");
 
 
        String method= httpServletRequest.getMethod();
 
        if (method.equals("OPTIONS")){
            httpServletResponse.setStatus(200);
            return false;
        }
 
        System.out.println(method);
 
        return true;
    }
 
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
 
    }
 
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
 
    }
}

在spring-mvc.xml配置

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/**"></mvc:mapping>
        <bean class="linkjb.base.ProcessInterceptor"></bean>
    </mvc:interceptor>
</mvc:interceptors>

前台传值,后台接收为null

没有把值真正的转换过去(传的参数类型有问题)

var params = new URLSearchParams();

params.append('userName', "qwwe12111");
params.append('pw', "qwwe12111");


全部评论: 0

    我有话说:

    咨询问题

    点击这里给我发消息

    淘宝咨询

    博主承接代做业务

    真·送云服务器

    咨询博主