0

我正在使用 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();
        }
    }
4

1 回答 1

2

我在 Win7/IE10 上测试了以下代码,并按预期收到了响应。您是否对服务器进行了任何更改?您是否仍然遇到问题?

appAPI.request.post({
    url: 'http://183.82.102.245:8020/Service1.svc/json/GetAffiliatedUrlsCollection',
    onSuccess: function(response) {
        alert(response);
    },        
    onFailure: function(httpCode) {
        alert('Failed to retrieve content. (HTTP Code:' + httpCode + ')');
    },
    contentType: 'application/json; charset=UTF-8; charset-uf8'
});

[披露:我是 Crossrider 的员工]

于 2014-03-23T12:37:44.303 回答