has been blocked by CORS policy: No ‘Access-Control-Allow-Origin‘ header is present on the requested

index.html:1 Access to XMLHttpRequest at 'http://127.0.0.1:8080/doGetLogin?username=admin&password=123456' from origin 'http://127.0.0.1:8848' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

 

大意是:

索引.html:1在'http://127.0.0.1:8080/doGetLogin?username=admin&password=123456“源站”http://127.0.0.1:8848'已被CORS策略阻止:请求的资源上不存在“Access Control Allow Origin”标头。

 

跨域问题

可以在请求的方法上添加注解:@CrossOrigin

    @CrossOrigin
    @RequestMapping(value="/doGetLogin",method = RequestMethod.GET)
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      
        String name=request.getParameter("username");

        String password=request.getParameter("password");

        System.out.println("姓名:"+name+",密码:"+password);

        if("admin".equals(name)&&"123456".equals(password)){
            System.out.println("请求成功!");

            response.getOutputStream().write("login success".getBytes());
        }else {
            response.getOutputStream().write("login failed".getBytes());
        }
    }

就可以解决该错误。

你可能感兴趣的:(前端,前端与后台交互,jquery,html)