9

这个问题开始了与IE9,其中对于POST要求,contentType必须是text/plain,并application/json将无法​​正常工作。我已添加moonscript并继续使用contentType: text/plain。我还添加了自定义介质类型的API,如对各种形式如下:text/plain的媒体类型不被接受的WebAPI V2

,并添加了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

+0

尝试更改'config.Formatters.JsonFormatter.SupportedMediaTypes.Add(新的MediaTypeHeaderValue(“文本/ HTML”));'到'config.Formatters.JsonFormatter.SupportedMediaTypes.Add (新的MediaTypeHeaderValue(“text/plain”));' –

+0

'TextMediaTypeFormatter'类增加了(在我从其他表单发布的(2)链接中) –

+0

只是一个猜测 - 但在阅读这两篇文章之后,得到为什么要添加text/html到JsonFormatter的原因,我的意思是第一行,而不是TextMediaTypeFormatter的添加 –

回答

1

我想你碰上的是,根据this MSDN blog

注意在2014年出现了奇怪的XDomainRequest问题:截至2014年,XDomainRequest似乎并没有发送任何Content-Type头。这个变化时我不清楚。

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

这也由您正在使用的jQuery扩展的文档进行备份。在自述文件中。MD

XDomainRequest有一定的局限性:

  • 存在请​​求

所以没有Content-Type头,如果你检查HttpContext.Request.ContentType我打赌这将是空/在这种情况下,你应该能够分配“text/plain”的响应类型,并向其工作的神祈祷。

基本上IE浏览器< 10支持XDomainRequest(甚至XDomainRequest本身)是垃圾。它已基本上是我的理解和IE 10 implemented CORS support for XHR requests

+0

我会检查出来 - 谢谢你的回应 –