这个问题始于IE9,对于
我添加了moonscript并继续使用
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?
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önningTextMediaTypeFormatter
类添加了我从其他表单中发布的链接(2)中的内容。 - Rob ScottdataType: 'json'
或contentType:'application/json',并在Chrome高级REST工具中检查您的响应。希望对您有所帮助。 - jayesh dhameliyaXDomainRequest
对象是否存在(IE9),contentType
会从json
切换到text/plain
。请注意,为了在IE9中进行POST,需要使用xhr
包装器。 - Rob Scott