2015-09-25 52 views
1

因此,我創建了一個使用CORS與.NET MVC Sidecar項目進行通信的應用程序。除了在POST請求失敗的IE9中,一切都很好。我有一個library它允許GETs工作,但POST請求仍然有問題。看來IE9會去掉contentType標題。服務器最終將其解釋爲appplication/octet-stream,而不是text/plain,這正是我所瞭解的實際情況。CORS在IE9中失敗

裏面我WebAPIConfig.cs的,我有以下功能(由Register叫):

private static void RegisterFormatters(HttpConfiguration config) 
    { 
     // enable browser viewing + jsonp 
     config.Formatters.Add(new BrowserJsonFormatter()); 
     config.Formatters.Add(new JsonpMediaTypeFormatter(new BrowserJsonFormatter())); 

     // prettify json: indented + camel case 
     var json = config.Formatters.JsonFormatter; 
     json.SerializerSettings.Formatting = Formatting.Indented; 
     json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); 

     // Tell the formatter to accept text/plain 
     config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/plain")); 
     // When we use CORS on IE9, it strips off the content type. The server assigns it application/octet-stream 
     config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/octet-stream")); 
    } 

然而,提琴手仍然告訴我,請求不具有的contentType,並且服務器無法處理application/octet-stream的contentType。

有沒有人得到這個工作?

+0

F12工具告訴你什麼?同樣的,我從來不信任IE9工具以外的任何信息,然後你仍然不能相信他們會告訴你所有事情。如果你需要虛擬機,請訪問http://modern.ie –

回答

0

我結束了創建我自己的MediaTypeFormatter奏效無線解決這個th application/octet-stream。 (我的服務器不接受電話,因爲我沒有做的IIS重置(廢話),但它仍然沒有被正確解析的對象。

這是我結束了使用的格式。

public class OctetStreamMediaTypeFormatter : MediaTypeFormatter 
{ 
    public OctetStreamMediaTypeFormatter() 
    { 
     // We only support octet-stream 
     SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/octet-stream")); 
    } 

    public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger) 
    { 
     var task = new Task<object>(() => 
     { 
      using (var reader = new StreamReader(readStream, Encoding.UTF8)) 
      { 
       // The data is passed as somethign identical to a query string (but not in the query string) 
       var dic = HttpUtility.ParseQueryString(reader.ReadToEnd()); 
       var json = JsonConvert.SerializeObject(dic.AllKeys.ToDictionary(k => 
       { 
        if (k.EndsWith("[]")) 
        { 
         return k.Substring(0, k.IndexOf("[]", StringComparison.OrdinalIgnoreCase)); 
        } 
        else 
        { 
         return k; 
        } 
       }, k => 
       { 
        var val = dic[k]; 
        // We need to speciallly handle arrays 
        if (k.EndsWith("[]")) 
        { 
         // parse into separate values; 
         var values = val.Split(','); 
         val = values.Aggregate("[", (current, value) => current + ("'" + value + "',")); 
         if (val.EndsWith(",")) 
         { 
          val = val.Substring(0, val.Length - 1); 
         } 
         val += "]"; 
        } 
        return val; 
       })); 
       // It still puts it in as a string, which is not what we want, so we need to remove the quotes 
       json = json.Replace("\"[", "[").Replace("]\"", "]"); 
       var obj = JsonConvert.DeserializeObject(json, type); 
       return obj; 
      } 
     }); 

     task.Start(); 

     return task; 
    } 

    public override bool CanReadType(Type type) 
    { 
     return true; 
    } 

    public override bool CanWriteType(Type type) 
    { 
     return true; 
    } 
} 
0

根據http://caniuse.com/cors CORS由以下瀏覽器支持:火狐3.5及以上版本,IE瀏覽器10+,谷歌Chrome 4.0+,Safari瀏覽器4.0以上版本, 歌劇院12.0或以上版本,以及Opera移動12.0或以上版本

XDomainRequest可能能夠幫助你(IE8 +)

<!DOCTYPE html> 
<html> 
<head> 
<title>XDR</title> 
<script type="text/javascript"> 
var xdr = new XDomainRequest(); 
xdr.open("get", "http://localhost:6504/api/resourcename/1"); 
xdr.onload=function() 
{ 
alert(xdr.responseText); 
} 
xdr.send(); 
</script> 

+0

我正在使用的庫已經爲我處理這個問題。獲取請求現在發生得很好。 –