9

這個問題開始了與IE9,其中對於POST要求,contentType必須是text/plain,並application/json將無法​​正常工作。我已添加moonscript並繼續使用contentType: text/plain。我還添加了自定義介質類型的API,如對各種形式如下:text/plain的媒體類型不被接受的WebAPI V2

,並添加了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

+0

嘗試更改'config.Formatters.JsonFormatter.SupportedMediaTypes.Add(新的MediaTypeHeaderValue(「文本/ HTML」));'到'config.Formatters.JsonFormatter.SupportedMediaTypes.Add (新的MediaTypeHeaderValue(「text/plain」));' –

+0

'TextMediaTypeFormatter'類增加了(在我從其他表單發佈的(2)鏈接中) –

+0

只是一個猜測 - 但在閱讀這兩篇文章之後,得到爲什麼要添加text/html到JsonFormatter的原因,我的意思是第一行,而不是TextMediaTypeFormatter的添加 –

回答

1

我想你碰上的是,根據this MSDN blog

注意在2014年出現了奇怪的XDomainRequest問題:截至2014年,XDomainRequest似乎並沒有發送任何Content-Type頭。這個變化時我不清楚。

Here's以前的關於該主題的SO問題以及哪個實際引用該博客。

這也由您正在使用的jQuery擴展的文檔進行備份。在自述文件中。MD

XDomainRequest有一定的侷限性:

  • 存在請​​求

所以沒有Content-Type頭,如果你檢查HttpContext.Request.ContentType我打賭這將是空/在這種情況下,你應該能夠分配「text/plain」的響應類型,並向其工作的神祈禱。

基本上IE瀏覽器< 10支持XDomainRequest(甚至XDomainRequest本身)是垃圾。它已基本上是我的理解和IE 10 implemented CORS support for XHR requests

+0

我會檢查出來 - 謝謝你的迴應 –