所以我创建了一个使用CORS与.NET MVC Sidecar项目进行通信的应用程序。一切都很好,除了在IE9中,POST请求失败。我有一个允许GET工作的library,但是POST请求仍然存在问题。似乎IE9剥离了contentType
标头。服务器最终将其解释为appplication/octet-stream
,而不是text/plain
,这是我理解的实际上是由它设置的。
在我的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"));
}
但是,Fiddler仍然告诉我,请求没有contentType,并且服务器无法处理application/octet-stream
的contentType。
有人有这个工作吗?
答案 0 :(得分:0)
根据http://caniuse.com/cors CORS受以下浏览器支持:Firefox 3.5 +,Internet Explorer 10 +,Google Chrome 4.0 +,Safari 4.0 +, Opera 12.0+和Opera Mobile 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>
答案 1 :(得分:0)
我最后通过创建与MediaTypeFormatter
一起使用的application/octet-stream
来解决此问题。 (我的服务器不接受这些调用,因为我没有完成IIS重置(duh),但它仍然无法正确解析对象。
这是我最终使用的格式化程序。
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;
}
}