发出POST请求时如何防止重定向到servlet?

时间:2019-06-20 08:44:49

标签: angular google-app-engine post google-cloud-platform

我正在尝试通过POST请求从本地主机网站向我的App Engine网站发送JSON,但我一直遇到CORS重定向错误:

  

从原点“ https://0-0-1-loader-dot-myproject.appspot.com/ml/startChangeUnmappedStatus”到“ http://localhost:8484”处对XMLHttpRequest的访问已被CORS策略阻止:对预检请求的响应未通过访问控制检查:预检请求不允许重定向

我尝试更改标头,使我的请求成为一个简单的请求,但是即使清空浏览器缓存后,我仍然遇到相同的错误。

一段时间后,我意识到出于安全原因,我的请求不是直接发送到我的servlet,而是发送到https://accounts.google.com/ServiceLogin?service=ah&passive=true&continue=https://appengine.google.com/_ah/conflogin%3Fcontinue%3Dhttps://0-0-1-loader-dot-myproject.appspot.com/ml/startChangeUnmappedStatus,后者经过身份检查后将重定向到我的servlet。

这意味着即使我编辑servlet的doOptions()以符合要求,由于OPTIONS请求已发送到https://accounts.google.com/

,也不会产生任何影响。

这是我的要求:

var data = json.encode(makeJSON(listPrediction)); //json containing my data

HttpRequest.request(
        'https://0-0-1-loader-dot-myproject.appspot.com/ml/startChangeUnmappedStatus',
    method: 'POST',
    sendData: data,
    requestHeaders: {
        'Content-Type':'application/x-www-form-urlencoded'
    }
)
    .then((resp) {
  print(resp.responseUrl);
  print(resp.responseText);
});

我的doOptions函数:

@Override
protected void doOptions(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
    resp.setStatus(HttpServletResponse.SC_OK);
    resp.addHeader("Access-Control-Allow-Origin", "*");
    resp.addHeader("Access-Control-Allow-Methods", "POST,GET,DELETE,PUT,OPTIONS");
    logger.info("hey i'm in the options");
}

那么,有没有办法避免这种重定向?我在做错什么吗?

0 个答案:

没有答案