這個問題開始了與IE9,其中對於POST
要求,contentType
必須是text/plain
,並application/json
將無法正常工作。我已添加moonscript並繼續使用contentType: text/plain
。我還添加了自定義介質類型的API,如對各種形式如下:text/plain的媒體類型不被接受的WebAPI V2
- 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(新的MediaTypeHeaderValue(「文本/ HTML」));'到'config.Formatters.JsonFormatter.SupportedMediaTypes.Add (新的MediaTypeHeaderValue(「text/plain」));' –
'TextMediaTypeFormatter'類增加了(在我從其他表單發佈的(2)鏈接中) –
只是一個猜測 - 但在閱讀這兩篇文章之後,得到爲什麼要添加text/html到JsonFormatter的原因,我的意思是第一行,而不是TextMediaTypeFormatter的添加 –