Posting text/plain as a complex object in WebAPI with CORS

后端 未结 2 808
生来不讨喜
生来不讨喜 2021-01-12 08:22

So I wish to POST (or PUT) a complex object to the server from a AJAX post using CORS. Unfortunately IE8 only supports sending text/plain when using CORS and not appli

相关标签:
2条回答
  • 2021-01-12 08:51

    If your WebApi application really only uses JSON, you could use this solution where it always responds with JSON and ignores the request content-type:

    How can I force asp.net webapi to always decode POST data as JSON

    From that, I would suggest this solution:

    This code needs to be added to Application_Start or WebApiConfig.Register

    config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/plain"));
    config.Formatters.Remove(config.Formatters.FormUrlEncodedFormatter);
    config.Formatters.Remove(config.Formatters.XmlFormatter);
    

    It tells the json formatter to accept the plain text content-type, and removes the form and xml formatters (although removing them may not be needed)

    0 讨论(0)
  • 2021-01-12 08:55

    ugly, but you could try modifying the content-type header from text/plain to application/json in a message handler so that parameter binding happens properly with json formatter.

    0 讨论(0)
提交回复
热议问题