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Ö÷ÒªÓÐÈýÖÖ£º×î¼òµ¥µÄÖ±½Ó¼ÓÔØÕû¸öÍøÒ³£¬È»ºóͨ¹ýinnerHTMLÖ®ÀàµÄ°ì·¨ÏÔʾÔÚÍøÒ³ÉÏ£¬Õâ¸öÏÖÔÚÓÃPrototype.jsµÄAjax.UpdaterÀàÄÜÇá¶øÒ×¾ÙµØʵÏÖ£»ÁíÒ»ÖÖ×î³£ÓõÄÊǼÓÔØÒ»¸öÖ»°üº¬Êý¾ÝµÄÎļþ£¨±ÈÈçXML£©È»ºóͨ¹ýһϵÁдúÂë´¦ÀíºóÏÔʾ¸øÓû§£»»¹ÓÐÒ»ÖÖÊÇÏñDWR»òPHPRPCÄÇÑùÖ±½Ó°Ñ·þÎñÆ÷¶ËµÄº¯ÊýÉú³É¶ÔÓ¦µÄJS´úÂë¸ø¿Í»§¶Ë£¬¿ ......
ÔÎĵØÖ·£ºhttp://sharethispoint.com/archive/2006/11/15/Build-web-parts-with-ajax.aspx
ÈçºÎÔÚ¿ª·¢Web²¿¼þʱʹÓÃajaxÄØ£¿ÎÒÃǽ«ÒÔÒ»¸öÑù×ÓÀàËÆMOSS 2007ÖÐKPIºÍBDC Web²¿¼þµÄWebPartΪÀýÀ´ËµÃ÷¡£Èç¹ûÄã¶ÔajaxÒ»µã¶¼²»Á˽â,ÍƼöѧϰTerryLeeµÄajaxÈëÃÅϵÁС£
ASP.net2.0ÓÐÒ»¸öºÜ¿áµÄÐÂÌØÐÔ½Ð×÷¿Í»§¶Ë½Å±¾»Øµ÷¡£½Å±¾»Øµ ......
Ç°ºó¶ËµÄ½»»¥ÊÇÕû¸ö¿ò¼ÜµÄÖÐÐÄ£¬ÎÒÏ£Íû´ÓÁ÷µÄ½Ç¶ÈÀ´Éè¼ÆAJAXµÄ½»»¥
AJAXµÄÉè¼Æ½«ºá¹áÇ°ºó¶Ë
Ç°¶ËÊÇÖ÷¶¯
ºó¶ËÊDZ»¶¯
ÕûÌ忼ÂǵĻ° Ç°ºó¶ËʹÓÃÒ»¸öͳһµÄ½Ó¿Ú½øÐÐAJAX½»»¥
ºó¶Ë ÓÃÒ»¸öΨһµÄURLÀ´´¦Àí AJAXÇëÇó
½Ó¿ÚÃû³Æ ´¦Àí½Ó¿Ú£º[http://ÓòÃû/mvc.ajax] ·â×°ËùÓеÄAJAXÇëÇóµÄÔ¤´¦Àí
......
var xmlHttp;
// ´´½¨XMLHttpRequest¶ÔÏó
function createXMLHttpRequest() {
try {
// FireFox, Opera 8.0 +, Safari
xmlHttp = new XMLHttpRequest();
}
catch ......