代码之家  ›  专栏  ›  技术社区  ›  user8438161

后服务器返回状态代码:415不支持的媒体类型(飞行前请求)

  •  1
  • user8438161  · 技术社区  · 7 年前

    Server API Cors Error 看看我昨天面临的问题,现在回答。

    现在我可以根据需要获取响应头,但现在的状态是 状态代码:415不支持的媒体类型 来自之前的状态代码:302身份验证。 服务器也应该返回一个对象,但不幸的是,它返回了'

    下面是返回方法POST时要返回的对象的响应,当涉及到请求方法-选项时,我收到了正确的状态-200(ok)

    响应-标题如下--

    Access-Control-Allow-Origin:*
    Cache-Control:no-cache
    Content-Length:0
    Date:Thu, 07 Sep 2017 09:59:22 GMT
    Expires:-1
    Pragma:no-cache
    Server:Microsoft-IIS/8.5
    X-AspNet-Version:4.0.30319
    X-Powered-By:ASP.NET
    

    Accept:application/json, text/plain
    Accept-Encoding:gzip, deflate
    Accept-Language:en-US,en;q=0.8
    file-token : xxxxxxxxxxxxxxxxx
    Connection:keep-alive
    Content-Length:44
    Content-Type:application/json
    Host: xxxxxxxxxxxxxxx
    Origin: http:xxxxxxxxxxxxxxxx
    Referer: http:xxxxxxxxxxxxxxxxxxxxxxxxxxxx
    User-Agent:Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36
    

    一般要求

    URL: http://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    Request Method:POST
    Status Code:415 Unsupported Media Type
    Remote Address: xxxxxxxxxxxxxxxxxxxxxxxxxx
    Referrer Policy:no-referrer-when-downgrade
    
    3 回复  |  直到 7 年前
        1
  •  0
  •   Kalamarico Gopakumar AP    7 年前

    问题是,您需要添加的CORS标头很少:

    Access-Control-Allow-Origin: *
    Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE
    Access-Control-Allow-Headers: Content-Type
    

    您的服务器正在正确添加Access Control Allow Origin标头,但您需要更多。在 link

    因此,为了确保在此飞行前请求中不会出现更多问题,您应该在服务器端添加Access Control Allow标头和Access Control Allow Methods标头。

        2
  •  0
  •   Cody Gray    7 年前

    解决方案是确保我们在标题中定义Accept和内容类型。例如:

    headers : { 'Content-type' : 'application/json', Accept : 'application/json'}
    
        3
  •  0
  •   Ricardo de Assuncao Goncalves    5 年前

    我收到了这个错误 JSON 未经加工的 “数据”选项卡,然后选择选项“ JSON(应用程序/JSON) "

    enter image description here

    我希望这有帮助。