【问题标题】:text/plain Media Type not being accepted for WebApi v2WebApi v2 不接受 text/plain 媒体类型
【发布时间】:2017-01-15 07:43:38
【问题描述】:

这个问题从 IE9 开始,对于 POST 请求,contentType 必须是 text/plain,而 application/json 将不起作用。

我添加了moonscript 并继续使用contentType: text/plain。我还在 api 中添加了自定义媒体类型,如下面的众多表单所示:

并将text/plain媒体类型的插入添加到WebApiConfig

config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;

// allows 'text/plain' as a supported media type
config.Formatters.Add(new TextMediaTypeFormatter());

但是,在 IE9 中发帖时(使用仿真),我仍然收到 415 Unsupported Media Type

Key Value Response HTTP/1.1 415 Unsupported Media Type

$.ajax({
    type: "POST",
    url: hope_forms.viivApiUrl + 'newsletter',
    contentType: 'text/plain',
    data: JSON.stringify(model),
    success: function (data) {
           .....
    },
    error: function (responseText) {
           console.log(responseText)
           modal.showModal('Something went wrong, please try again.');
   }                    
});

加法:

如果出现问题,这里是完整的WebApiConfig

var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

// Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
// To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
// For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
//config.EnableQuerySupport();

config.EnableSystemDiagnosticsTracing();


//config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;

// allows 'text/plain' as a supported media type
config.Formatters.Add(new TextMediaTypeFormatter());

我还更改了 ajaxTransport xhr 包装器以改用它: https://github.com/gfdev/javascript-jquery-transport-xdr


注意:

截至今天 09/21,我已将我的所有 POST 请求切换为 GET,但我仍然希望通过变通方法将这些类型恢复为 POST

【问题讨论】:

  • 尝试将config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));改为config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/plain"));
  • TextMediaTypeFormatter 类添加了这一点(在我从其他表单发布的 (2) 个链接中。
  • 只是一个猜测-但在阅读了这两篇文章后,我无法理解为什么要向 JsonFormatter 添加 text/html。我的意思是第一行,而不是添加 TextMediaTypeFormatter。
  • @Rob Scott 请提前检查您的 web api 响应休息客户端或邮递员与您的发布 URL,因为当 web api 抛出错误时,它会返回 html 响应 [AdvanceRest](chrome.google.com/webstore/detail/advanced-rest-client/…)
  • 添加 ajax 调用 dataType: 'json' 或 contentType:'application/json' 并在 chrome 高级休息工具中检查您的回复,希望对您有所帮助

标签: c# jquery asp.net-web-api internet-explorer-9 mediatypeformatter


【解决方案1】:

我认为您遇到了 this MSDN blog 于 2014 年出现的奇怪 XDomainRequest 问题

注意:截至 2014 年,XDomainRequest 似乎根本没有发送任何 Content-Type 标头。我不清楚这是什么时候改变的。

Here's 之前关于该主题的 SO 问题以及实际上引用了该博客。

您正在使用的 jQuery 扩展的文档也支持这一点。在 Readme.md 中

XDomainRequest 有一些限制:

  • 请求中没有 Content-Type 标头

因此,如果您检查HttpContext.Request.ContentType,我敢打赌它将为空/空,在这种情况下,您应该能够分配“文本/纯文本”的响应类型并祈祷它有效。

基本上 IE IE 10 implemented CORS support for XHR requests

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-12
    • 2020-09-30
    • 2021-02-28
    • 2013-12-24
    • 2021-12-24
    • 2020-09-20
    • 2020-02-26
    • 1970-01-01
    相关资源
    最近更新 更多