【问题标题】:405 method not allowed using Tomcat不允许使用 Tomcat 的 405 方法
【发布时间】:2018-12-10 06:19:47
【问题描述】:

我是 Web 开发的新手,遇到这个问题已经有一段时间了。我尝试了很多关于 SO 和其他地方的建议,但没有运气。不管怎样,我会解释我在做什么。

我正在尝试一个简单的练习,其中我有一个简单的 HTML 页面,它接受一个城市名称,然后单击提交按钮,将显示该城市的天气。 HTML代码如下:

<body>
    <form>
        <div>
            <label for="city">City</label>
            <input id="city" type="text" name="city">
        </div>
        <div>
            <input id="submitButton" type="button" name="submitButton" value="Get weather!" onclick="send()">
        </div>
    </form>
    <script type="text/javascript" src="CityWeather.js"></script>
</body>

此 HTML 文件调用我的 JS 脚本,该脚本将处理对 OpenWeatherMap API 的调用并显示结果。 JS的代码如下(请注意我故意省略了api_key,但实际的URL确实有):

function send() {
    var xhttp = new XMLHttpRequest();
    xhttp.open("GET", "http://api.openweathermap.org/data/2.5/weather?q=london&APPID=<api_key>", true);
    xhttp.setRequestHeader("Content-type", "application/json");
    xhttp.send();
    var jsonResponseText = xhttp.responseText;
    //var response = JSON.parse(jsonResponseText);
    console.log(xhttp.readyState);
    console.log(xhttp.status);
    console.log(jsonResponseText);
}

现在,当我第一次尝试仅通过启动 HTML 文件来获取结果时,我遇到了 CORS 问题,我意识到我不应该通过文件 URL(file://) 调用 API。所以我下载了 Tomcat 9.0 并将 HTML 和 JS 文件都放在了 Tomcat 的 webapps 文件夹中。然后我启动了 Tomcat 并启动了该页面。但是,我得到了下面提到的例外情况:

OPTIONS http://api.openweathermap.org/data/2.5/weather?q=london&APPID=<api_key> 405 (Method Not Allowed)

Failed to load http://api.openweathermap.org/data/2.5/weather?q=london&APPID=<api_key>: Response for preflight has invalid HTTP status code 405.

我尝试使用以下条目更新 CATALINA_BASE/conf/web.xml 文件,

<filter>
  <filter-name>CorsFilter</filter-name>
  <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>CorsFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

重新启动服务器并再次尝试,但仍然没有运气。有人可以通过指出需要做什么来帮助我吗?我正在使用 Chrome 进行此练习。我也尝试过使用 IE,但没有运气。

更新 1: 我尝试更改 api 调用。我没有使用 openweathermap api,而是尝试使用 GIT API 来获取我的存储库。

function send() {
    var xhttp = new XMLHttpRequest();
    xhttp.open("GET", "https://api.github.com/users/bhavyas66", true);
    xhttp.setRequestHeader("Content-type", "application/json");
    xhttp.send();
    var jsonResponseText = xhttp.responseText;
    //var response = JSON.parse(jsonResponseText);
    console.log(xhttp.readyState);
    console.log(xhttp.status);
    console.log(jsonResponseText);
}

在这种情况下,我没有得到上面的异常,但是状态卡在1,打印的状态为0。响应也是空的。但是,当我打开 Chrome 网络窗口时,我可以看到预览选项卡下有响应

【问题讨论】:

  • 为什么要在 GET 请求中添加“Content-type: application/json”标头?除了触发浏览器发出 CORS 预检 OPTIONS 请求之外,将该标头添加到请求中没有任何作用。从您的请求中删除该标头。
  • 在自己的服务器上进行什么 CORS 配置并不重要。请参阅stackoverflow.com/questions/50518345/… 的评论。您必须改为从后端执行请求。您正在向 openweathermap.org API 发出请求,该 API 无法正确处理 CORS preflight OPTIONS 请求。如果在向该 API 发出请求时,您在请求中添加了“Content-Type: application/json”标头,这会触发浏览器发送 CORS 预检 OPTIONS 请求,而该 API 无法处理

标签: javascript tomcat cors http-status-code-405


【解决方案1】:

尝试“POST”方法,405错误经常与POST方法同时出现

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-01
    • 2012-06-29
    • 2013-03-13
    • 2016-03-08
    • 2014-09-19
    • 2016-04-20
    相关资源
    最近更新 更多