2

我想我已经阅读了关于 XDomainRequest 的所有 StackOverflow 帖子以及关于 AJAX 和 WCF 的另外几十篇文章,但我仍然无法让XDomainRequest AJAX 调用正常工作。我已经在我的 WCF 服务上实现了 CORS(“Access-Control-Allow-Origin”),并且我的代码在 Chrome 和 Firefox 中与 xmlHttpRequest 一起工作得很好,但是我正在跨域进行调用,所以对于 IE,我需要使用XDomainRequest 对象。当我 GET 或 POST 到没有 args 的方法时,我的 xdr 工作正常,我什至可以使用查询字符串成功地将 GET 动词用于具有 args 的方法,但是当我尝试POST具有 args的方法时我的 xdr 抛出一个错误,即使我在 BeginRequest 方法中放置了一个断点并且我看到来自服务器的响应是“200 OK”。我想我已经尝试了配置文件设置的每一种组合,但我必须遗漏一些东西。非常感谢为我指明正确方向的任何帮助。

以下是我的代码的相关部分:

WCF - Global.asax

protected void Application_BeginRequest(object sender, EventArgs e)
    {
        //for CORS
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
    }

WCF - IService1.cs

[ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebInvoke(Method = "POST")]
    string GetData();

    [OperationContract]
    [WebInvoke(Method = "POST")]
    string GetData2(string param);
}

WCF-Service1.svc

public class Service1 : IService1
{
    public string GetData()
    {
        return "Hello";
    }

    public string GetData2(string param)
    {
        return string.Format("Hello - {0}", param);

    }
}

WCF - Web.config

<?xml version="1.0"?>

<system.web>
    <compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    <services>
        <service behaviorConfiguration="WcfService1.Service1Behavior" name="WcfService1.Service1">
            <endpoint address="AjaxEndpoint" behaviorConfiguration="AjaxBehavior" contract="WcfService1.IService1" bindingConfiguration="AjaxBinding"/> 
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="WcfService1.Service1Behavior">
                <!--<serviceMetadata httpGetEnabled="true" />-->
                <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
        </serviceBehaviors>
        <endpointBehaviors>
            <behavior name="AjaxBehavior">
                <enableWebScript/>
            </behavior>
        </endpointBehaviors>
    </behaviors>
    <bindings>
        <webHttpBinding>
            <binding name="AjaxBinding"/>
        </webHttpBinding>
    </bindings>
</system.serviceModel>
<system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>

客户端 AJAX 调用

var WcfURL = "http://localhost/WcfService1/Service1.svc/AjaxEndpoint"
if (window.XDomainRequest) {
//IE - use cross-domain request
xdr = new XDomainRequest();
xdr.onprogress = function () { alert("onprogress: " + xdr.responseText) };
xdr.onload = function () { updateText(xdr.responseText) }; 
xdr.onerror = function () { alert("xdr error") };
xdr.timeout = 7500;
xdr.ontimeout = function () { alert("xdr timeout") };

var data = "passedInParam";
//var method = "/GetData";  //this works
var method = "/GetData2";  //this throws an error
xdr.open("POST", WcfURL + method);

xdr.send(data);

}

4

1 回答 1

4

我找到了解决方案。感谢 Fiddler,我能够查看更多从服务返回的响应。错误是

传入消息具有意外的消息格式“原始”。该操作的预期消息格式为“Xml”、“Json”。这可能是因为尚未在绑定上配置 WebContentTypeMapper。

有了这些信息,我开始研究 WebContentTypeMapper。我发现这篇文章很有用,在添加 WebContentTypeMapper 方法后,我可以看到来自 XDomainRequest 的请求的 contentType 类型为“application/json”(如预期的那样),而我没有在 XDomainRequest.send() 方法中包含一个参数,但在传入参数时更改为类型“application/octet-stream”。(即 xdr.send(data))我不知道为什么它会更改为八位字节-stream,但这样做会导致服务方法抛出 500 错误(带有上面的消息),从而导致 xdr 请求出错。但是 WebContentTypeMapper 被恰当地命名,并且使用它来更改 contentType 很容易。在将其更正为 Json 类型后,我的 xdr 运行良好。

这是方法:

public class CustomContentTypeMapper : WebContentTypeMapper
{
    public override WebContentFormat GetMessageFormatForContentType(string contentType)
    {
        if (contentType == "application/octet-stream")
        {
            return WebContentFormat.Json;
        }
        else
        {
            return WebContentFormat.Default;
        }
    }
}

以下是更新的配置文件部分:

<endpoint address="AjaxEndpoint" behaviorConfiguration="AjaxBehaviour" contract="WcfService1.IService1" binding="customBinding" bindingConfiguration="CustomMapper"/>
...
<bindings>
        <customBinding>
            <binding name="CustomMapper">
                <webMessageEncoding webContentTypeMapperType="WcfService1.CustomContentTypeMapper, WcfService1" />
                <httpTransport manualAddressing="true" />
            </binding>
        </customBinding>
    </bindings>
于 2012-06-22T15:03:43.557 回答