我正在向我的服务器发送请求,使用$ http到我的后端进行登录,但即使在正确指定了标题之后,我的请求还要添加标题,我也从未问过它。
这是我对服务器的请求
$http({
method: 'POST',
url: 'http://localhost:8080/SignInMainServlet',
data: $httpParamSerializerJQLike({
login_source: source,
accessToken: code,
referrer_code: referral_id
}),
headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' }
});
这是我处理请求的服务器代码
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
REFERRAL_CODE_COUNT++;
String referrer_code=request.getParameter("referrer_code");
String login_source=request.getParameter("login_source");
String accessToken=request.getParameter("accessToken");
SignInMainManager m = new SignInMainManager(accessToken, referrer_code, login_source,REFERRAL_CODE_COUNT);
String result = m.signIn();
response.setContentType("text/html");
response.addHeader("Access-Control-Allow-Origin","*");
response.addHeader("Access-Control-Allow-Methods","POST");
response.addHeader("Access-Control-Allow-Headers","Content-Type");
PrintWriter out = response.getWriter();
out.println(result);
out.close();//closing the stream
}
这是浏览器控制台显示的错误
XMLHttpRequest cannot load http://localhost:8080/SignInMainServlet. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.
这是请求,响应和一般在xhr调用的chrome上的样子
// Request//
OPTIONS /SignInMainServlet HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: http://localhost:3000
User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.76 Mobile Safari/537.36
Access-Control-Request-Headers: authorization
Accept: */*
Referer: http://localhost:3000/
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
//Response//
HTTP/1.1 200 OK
Date: Sun, 05 Feb 2017 10:44:57 GMT
Allow: POST, TRACE, OPTIONS
Content-Length: 0
Server: Jetty(8.1.14.v20131031)
//General//
Request URL:http://localhost:8080/SignInMainServlet
Request Method:OPTIONS
Status Code:200 OK
Remote Address:127.0.0.1:8080
从一天起就被困在这里。真的需要帮助!!
答案 0 :(得分:2)
我发现你正在使用authentification
。我相信您的预检请求无法正常工作,因为您错过了CORS中的某些配置。这样你的预检请求就会中止。像这样设置您的CORS:
response.addHeader("Access-Control-Allow-Origin","*");
response.addHeader("Access-Control-Allow-Methods","GET, POST, OPTIONS");
response.addHeader("Access-Control-Allow-Credentials", "true");
response.addHeader("Access-Control-Allow-Headers","Origin, Content-Type, X-Auth-Token , Authorization");
最重要的是,您只配置了protected void doPost()
侦听器。通过这种方式,预检OPTIONS
请求没有配置CORS,因为它正在监听doOptions()
。添加一个doOptions()
侦听器并将您的CORS配置放入其中或定义您的response
CORS全局。
protected void doOptions(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.addHeader("Access-Control-Allow-Origin","*");
response.addHeader("Access-Control-Allow-Methods","GET, POST, OPTIONS");
response.addHeader("Access-Control-Allow-Credentials", "true");
response.addHeader("Access-Control-Allow-Headers","Origin, Content-Type, X-Auth-Token , Authorization");
PrintWriter out = response.getWriter();
out.close();//closing the stream
}