【发布时间】:2017-01-15 07:43:38
【问题描述】:
这个问题从 IE9 开始,对于 POST 请求,contentType 必须是 text/plain,而 application/json 将不起作用。
我添加了moonscript 并继续使用contentType: text/plain。我还在 api 中添加了自定义媒体类型,如下面的众多表单所示:
- http://www.stormbase.net/2015/09/21/webapi-post-plaintext/
- how to post plain text to ASP.NET Web API endpoint?
并将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