我正在使用 crossrider 创建浏览器扩展。此扩展安装完成后,需要调用 wcf 服务,它在 chrome、firefox 和 safari 中运行良好,但在 IE 中显示 badrequest 错误,即错误 400。以下是我的 Crossrider 代码
appAPI.request.post({
url: 'http://183.82.102.245:8020/Service1.svc/json/GetAffiliatedUrlsCollection',
onSuccess: function(response) {var site = appAPI.JSON.parse(response);
AddUrlsToDB(site);
},
onFailure: function(httpCode) {
alert('Failed to retrieve content. (HTTP Code:' + httpCode + ')');
},
additionalRequestHeaders: {
myHeader: 'value'
},
contentType: 'application/json'
});
以下是我的服务代码
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "GetAffiliatedUrlsCollection", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json)]
List<UrlInfo> GetAffiliatedUrlsCollection();
以下是我的属性
[DataContract]
public class UrlInfo
{
private string affiliatedurl;
public string websiteurl;
public bool autoapprove;
public UrlInfo(string websiteurl, string affiliatedurl, bool autoapprove)
{
this.websiteurl = websiteurl;
this.affiliatedurl = affiliatedurl;
this.autoapprove = autoapprove;
}
[DataMember]
public string WebsiteUrl
{
get { return websiteurl; }
set { websiteurl = value; }
}
[DataMember]
public string AffilateUrl
{
get { return affiliatedurl; }
set { affiliatedurl = value; }
}
[DataMember]
public bool AutoApprove
{
get { return autoapprove; }
set { autoapprove = value; }
}
}
然后是 Globla.asax
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, x-requested-with");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}