gpt4 book ai didi

ajax - 如何使用参数将 XDomainRequest 调用发布到 WCF 服务方法

转载 作者:行者123 更新时间:2023-12-04 18:18:56 26 4
gpt4 key购买 nike

我想我已经阅读了 XDomainRequest 上的每篇 StackOverflow 帖子以及 AJAX 和 WCF 上的另外几十篇文章,但我仍然无法获得 。 XDomainRequest AJAX 调用 去工作。我已经在我的 WCF 服务上实现了 CORS(“Access-Control-Allow-Origin”),并且我的代码在 Chrome 和 Firefox 中与 xmlHttpRequest 一起工作得很好,但是我正在跨域进行调用,所以对于 IE,我需要使用XDomainRequest 对象。当我 GET 或 POST 到没有 args 的方法时,我的 xdr 工作正常,我什至可以使用查询字符串成功地将 GET 动词用于具有 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);

}

最佳答案

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

The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'. This can be because a WebContentTypeMapper has not been configured on the binding.



有了这些信息,我开始研究 WebContentTypeMapper。我找到了 this文章很有用,在添加 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>

关于ajax - 如何使用参数将 XDomainRequest 调用发布到 WCF 服务方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11105495/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com