Ten problem rozpoczął się od IE9, gdzie dla żądań POST
musi być text/plain
, a application/json
nie będzie działać.text/plain Media Typ nie jest akceptowany dla WebApi v2
Dodałem moonscript i przystąpiłem do korzystania z contentType: text/plain
. Dodałem także niestandardowy typ nośnika do API, jak pokazano na licznych formach poniżej:
- http://www.stormbase.net/2015/09/21/webapi-post-plaintext/
- how to post plain text to ASP.NET Web API endpoint?
I dodaje wstawiania typu text/plain
mediów do 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());
Jednak w przypadku publikowania w IE9 (przy użyciu emulacji) nadal otrzymuję numer 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.');
}
});
Dodatek:
Oto pełnowartościowy WebApiConfig
w przypadku, gdy coś jest nie w porządku:
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());
Ja również zmienił ajaxTransport xhr
opakowanie używać to zamiast : https://github.com/gfdev/javascript-jquery-transport-xdr
Uwaga:
dzień dzisiejszy, 09/21, mam włączone wszystkie moje POST
żądania GET
, ale nadal chcieliby obejść, aby te typy z powrotem do POST
.
Spróbuj zmienić 'config.Formatters.JsonFormatter.SupportedMediaTypes.Add (nowa MediaTypeHeaderValue ("text/html"));' na 'config.Formatters.JsonFormatter.SupportedMediaTypes.Add (nowa MediaTypeHeaderValue ("text/plain")); ' –
Klasa' TextMediaTypeFormatter' dodaje, że (która jest w (2) odsyłaczach, które zamieściłem z innych formularzy. –
Zgadnij - ale po przeczytaniu obu artykułów nie mogę powód, dla którego należy dodać tekst/html do JsonFormatter Mam na myśli pierwszą linię, a nie dodanie TextMediaTypeFormatter . –