【问题标题】:AJAX request returning "405 Method Not Allowed"AJAX 请求返回“405 Method Not Allowed”
【发布时间】:2018-04-17 00:17:12
【问题描述】:

我正在尝试编写一个提交表单,将 JSON 对象发送到另一台服务器上的应用程序,但我在控制台中不断收到 405 Method Not Allowed 错误。

应用程序设置为接受POST,这就是我要发送的内容,所以我迷失了错误所在。控制台中也有警告Loading failed for the <script> with source "Request URL"

这是应用程序读取 JSON 的方式或发送 JSON 的格式有问题吗?

来自应用服务器

Failed to execute: javax.ws.rs.NotSupportedException: RESTEASY003065: Cannot consume content type 

代码

json = JSON.parse(string);
$.ajax({
   type: 'POST',
   dataType: "jsonp",
   contentType: "application/json",
   url: "http://test:8080/request/committee",
   data: json,
   cache: false,
   success: function (response) {
       $("#successModal").modal('show');
    },
    failure: function (response) {
       $("#failureModal").modal('show');
    },
    error: function (response) {
       $("#failureModal").modal('show');
                }
    });
    e.preventDefault();//prevents the form from being submitted by default

请求标头

Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.5
Connection: keep-alive
Host: test:8080
Referer: http://www.form.com/servlet/rsvp.jsp?location=110%20Road%20Bolton%20Landing,%20NY&purpose=%20TPAS%20Teleconference%20(RSVP%20here%20to%20attend%20in-person)&visitDate=2018-03-15&visitStartTime=6:00%20AM&visitEndDate=7:00%20AM&committee=all
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0

响应标头

HTTP/1.1 405 Method Not Allowed
Allow: POST, OPTIONS
Connection: keep-alive
Content-Length: 0
Date: Mon, 16 Apr 2018 15:38:24 GMT

编辑:更正contentType。出现同样的错误

【问题讨论】:

  • type: 'POST', -> method: 'POST'
  • 试试contentType: "application/json"。只使用 JSONP 作为返回类型,不用于发送。
  • 你为什么使用jsonp?您是否在使用常规 json 进行跨站点请求时遇到问题?
  • @Keith,很好。
  • 您不能在 JSONP 中使用 POST 请求。

标签: javascript jquery json ajax


【解决方案1】:

从您的异常详细信息来看,它似乎是 执行失败:javax.ws.rs.NotSupportedException:RESTEASY003065:无法使用内容类型

您传递的内容类型无效或根本不传递,我看到您的内容类型是 application/jsonp。 只需尝试一下 application/javascript。

【讨论】:

    【解决方案2】:

    这个错误发生在我身上一次。我的 java 方法想要 xml 而不是 json。这个注释(在 java 中)为我修复了它。

    @Consumes(MediaType.APPLICATION_JSON)
    

    我希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2016-12-04
      • 2014-09-03
      • 2011-09-25
      • 2014-11-26
      • 2016-10-16
      • 1970-01-01
      • 2015-07-01
      • 2011-05-14
      • 1970-01-01
      相关资源
      最近更新 更多