POST服务器返回状态码:415不支持的媒体类型(预检请求)

时间:2017-09-07 07:21:22

标签: javascript angularjs post http-post

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

我发送的数据应该是' xlsx文件'预计服务器也

现在,我可以根据需要获取响应标头,但现在状态为状态代码:415以前的状态码不支持的媒体类型:302验证。 服务器也应该返回一个对象,但不幸的是它返回' 错误:[object Object] undefined'

如果需要对定义的代码进行任何引用,请参阅上面的链接' Server API Cors Error'

以下是方法POST返回的对象的响应,当涉及到Request Method - 选项时,我收到正确的状态 - 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 个答案:

答案 0 :(得分:0)

问题是您需要添加的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中,您有prefilight请求的cors规范,并且当内容类型时,它表示内容类型不是正常的标题不是application / x-www-form-urlencoded,multipart / form-data或text / plain,服务器必须允许。

因此,为了确保您在此预检请求中不会遇到更多问题,您应该在服务器端添加Access-Control-Allow-Headers和Access-Control-Allow-Methods标头。

答案 1 :(得分:0)

解决方案是确保我们定义Accept以及标头中的Content-type。例如:

headers : { 'Content-type' : 'application/json', Accept : 'application/json'}

答案 2 :(得分:0)

我收到此错误状态代码:415不支持的媒体类型,并且我在服务器端代码中注意到,它期望参数来自请求的正文,因此在PostMan中,我移动了身体的参数,它起作用了。请检查我在下图中创建的示例。确保在原始数据标签中传递了 JSON ,然后选择选项“ JSON(application / json)

enter image description here

希望对您有帮助。