Cross Domain AJAX Enabled WCF Service
Background For the basic of how to create an AJAX enabled WCF service, please refer to MSDN: http://msdn.microsoft.com/en-us/library/bb924552.aspx. For the basic of JSONP, please refer to: http://en.wikipedia.org/wiki/JSON#JSONP. This article introduce how to make AJAX enabled WCF service support cross-domain request. In the WCF & WF samples provided by Microsoft, there is already a “custom binding extension” implementation for JSONP support. But there is some limitation, the biggest limitation is it could not support both JSON & JSONP protocol for one AJAX enabled WCF service URL, and it requires each endpoint you want to support JSONP protocol be configured with the specific custom JSONP binding extension which increased configuration complexity & cost. Here I introduce a more general “custom Http Module” implementation for cross-domain calling AJAX enabled WCF services. At the beginning, firstly, please realize WCF does not support custom Http Modules by default, which means, by default, a WCF service request, even a webHttpBinding AJAX enabled WCF service request, doesn’t go through any custom Http Modules. To enable it, you need to set the aspNetCompatibilityEnabled property of serviceHostingEnvironment element to true like below:
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
...
</system.serviceModel>And to enable a WCF service support custom Http Module, you also need to mark the AspNetCompatibilityRequirementsAttribute on the service contract like below: [ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)]
public class TestAjaxClientService
{
...
}JSONPModule Since with the aspNetCompatibilityEnabled property set to true, an Http WCF service request goes through custom Http Modules, it is
相关文档:
最近网上提的很多的一个新概念就是 AJAX 了, 那么, AJAX 是什么呢? 以下内容引用网上资料:
AJAX全称为“Asynchronous JavaScript and XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术。它有机地包含了以下几种技术:
Ajax(Asynchronous JavaScript + XML)的定义
基于 web标准(sta ......
在构造url是用javascript自带的encodeURIComponent方法将参数进行编码,下面是我的代码
var url = "handel.jsp?name="+encodeURIComponent(document.form1.name.value);
httpRequest = createHttpRequest();
httpReque ......
var xmlhttp;
function verify()
{
//2.创建XmlHttpRequest对象
//这是XmlHttpRequest对象五步中使用最复杂的一步
//需要针对IE和其他类型的浏览器建立这个对象的不同方式写不同的代码
if(window.XMLHttpRequest)
{
......
var xmlHttp;
// 创建XMLHttpRequest对象
function createXMLHttpRequest() {
try {
// FireFox, Opera 8.0 +, Safari
xmlHttp = new XMLHttpRequest();
}
catch ......