appAPI.request.post适用于Chrome,Firefox和Safari,但不适用于IE浏览器

时间:2014-03-22 08:27:52

标签: jquery crossrider ajax-request

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

1 个答案:

答案 0 :(得分: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员工]