文本/纯文本媒体类型未被WebApi v2接受。

9
这个问题始于IE9,对于POST请求,需要将contentType设置为text/plain,而application/json则不可行。
我添加了moonscript并继续使用contentType: text/plain。我还按照下面多个表单所示,向api添加了自定义媒体类型: 并在WebApiConfig中添加了text/plain媒体类型的插入。
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不支持的媒体类型错误。

关键字 值 响应 HTTP/1.1 415 不支持的媒体类型

$.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")); - Ralf Bönning
TextMediaTypeFormatter类添加了我从其他表单中发布的链接(2)中的内容。 - Rob Scott
只是猜测 - 但在阅读了这两篇文章之后,我无法理解为什么要将text/html添加到JsonFormatter中。我的意思是第一行,而不是添加TextMediaTypeFormatter。 - Ralf Bönning
@Rob Scott,请使用Advance Rest Client或Postman在您的发布URL上提前检查您的Web API响应,因为当Web API抛出错误时,它会返回HTML响应。AdvanceRest - jayesh dhameliya
在ajax调用中添加dataType: 'json'或contentType:'application/json',并在Chrome高级REST工具中检查您的响应。希望对您有所帮助。 - jayesh dhameliya
根据XDomainRequest对象是否存在(IE9),contentType会从json切换到text/plain。请注意,为了在IE9中进行POST,需要使用xhr包装器。 - Rob Scott
1个回答

1
我想您遇到了2014年出现的奇怪的XDomainRequest问题,据this MSDN blog所述。

注意:截至2014年,XDomainRequest似乎根本不发送任何Content-Type标头。我不清楚这是何时发生的。

这里还有一个先前的SO问题,实际上引用了那篇博客。

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

XDomainRequest有一些限制:

  • 请求中没有Content-Type标头

因此,如果您检查HttpContext.Request.ContentType,我敢打赌它将为空/空,这种情况下,您应该能够分配“text/plain”的响应类型,并祈求上帝它能够工作。

基本上,IE < 10 对于XDomainRequest的支持(甚至是XDomainRequest本身)都很糟糕。据我了解,它已经被基本废弃,而IE 10实现了对XHR请求的CORS支持


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接