Communication Foundation (WCF) 来创建异步 JavaScript 和 XML (AJAX) 服务,该服务返回 JavaScript 对象表示法 (JSON) 或 XML 数据。可以从 Web 浏览器客户端使用 JavaScript 代码来访问 AJAX 服务。此示例是基于 基本 AJAX 服务示例生成的。
You can see that there's nothing in there that says anything about JSON support, yet we can configure this service to return JSON over HTTP instead of SOAP. .NET 3.5 introduces JSON serialization support for services without having to decorate your code with any special attributes, you just configure an endpoint to use the webHttpBinding, and add an endpoint behavior that enables web scripting.
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="AjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="Service">
<endpoint
address=""
binding="webHttpBinding"
contract="IService"
behaviorConfiguration="AjaxBehavior" />
</service>
</services>
</system.serviceModel>
Very cool, now our service is exposed and returns JSON over HTTP instead of XML over HTTP! You can now go to your service endpoint and append "/js" to the querystring to see the JSON proxy that is generated. For instance, the endpoint URL on my machine is:
http://localhost:49455/WCFServices/Service.svc/js
To call the service, ASP.NET 3.5's built-in AJAX support makes referencing the JavaScript proxy very simple.
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Scripts>
<asp:ScriptReference Path="~/Scripts.js" />
</Scripts>
<Services>
<asp:ServiceReference Path="~/Service.svc" />
</Services>
</asp:ScriptManager>
In this example, I am using the ScriptManager control in ASP.NET and pointing to our Service.svc host file. Thanks to the JavaScript Intellisense features in Visual Studio 2008, I get Intellisense completion so that I can figure out how to reference the proxy correctly.
Problem : Manual addressing is enabled on this factory, so all messages sent must be pre-addressed.
Reslove: client.Endpoint.Behaviors.Add(new System.ServiceModel.Description.WebHttpBehavior());
总体 解决:
client invoke the function.
这个问题的主要原因是因为 server(application/json) , client(application/xml, soap) 编码方式不一样,所以 server 返回来的消息client 无法解析. 改变 client 的binding,
The SOAPAction header is actually pretty common - I have a feeling it may even be required for SOAP 1.2, if not expected for SOAP 1.1 (asmx) it;s certainly pervasive in WCF when using the wsHttpBinding which is SOAP 1.2 (the basicHttpBinding is SOAP 1.1). Not strictly sharepoint related, but important to raise, since the services that don't require it are possibly not SOAP compliant. I say possibly because I'm not an authority on the subject and I don't know the spec for sure, I just know I've been caught out by this a lot when learning WCF.
In my ongoing quest to produce the simplest table possible summarizing the key differences between the various Windows Communication Foundation (WCF) built-in bindings, I came up with the following:
Binding Class Name | Transport | Message Encoding | Message Version | Security Mode | RM | Tx Flow* |
BasicHttpBinding | HTTP | Text | SOAP 1.1 | None | X | X |
WSHttpBinding | HTTP | Text | SOAP 1.2 WS-A 1.0 | Message | Disabled | WS-AT |
WSDualHttpBinding | HTTP | Text | SOAP 1.2 WS-A 1.0 | Message | Enabled | WS-AT |
WSFederationHttpBinding | HTTP | Text | SOAP 1.2 WS-A 1.0 | Message | Disabled | WS-AT |
NetTcpBinding | TCP | Binary | SOAP 1.2 | Transport | Disabled | OleTx |
NetPeerTcpBinding | P2P | Binary | SOAP 1.2 | Transport | X | X |
NetNamedPipesBinding | Named Pipes | Binary | SOAP 1.2 | Transport | X | OleTx |
NetMsmqBinding | MSMQ | Binary | SOAP 1.2 | Message | X | X |
MsmqIntegrationBinding | MSMQ | X** | X | Transport | X | X |
CustomBinding | You decide | You decide | You decide | You decide | You decide | You decide |
2. Content-Type
SOAP 1.1 HTTP/SOAP 消息的媒体类型为“text/xml”,其编码处理是在 RFC2376 中定义的。而 SOAP 1.2 HTTP/SOAP 消息的媒体类型为“application/soap+xml”,其编码处理是在 RFC3023 中定义的。这些 RFC 规范定义了下列行为: