2013-05-05 73 views
0

我想打電話給在asp.net使用jQuery WCF服務在asp.net從jQuery的調用WCF服務

WCF服務和asp.net網站是同一個解決方案下,但他們在不同的端口上運行如下面

WCF - http://127.0.0.1:54443/Service1.svc/ 
asp.net - http://127.0.0.1:57484/WebSite2/ 

下面是WCF功能碼

[WebInvoke(Method = "GET",RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json)] 

     public List<string> getCurrentTime() 
     { 
      List<string> lstrSampleDate = new List<string>(); 
      lstrSampleDate.Add(DateTime.Now.TimeOfDay.ToString()); 
      return lstrSampleDate; 
     } 

和下面是jQuery代碼

$.ajax({ 

       type: "GET", 
       url: "http://127.0.0.1:54443/Service1.svc/getCurrentTime", 
       // crossDomain: true, 
       datatype: "json", 
       success: function (rVal) { 
        //console.log 
        alert(rVal); 
       }, 
       error: function (xhr, status) { 
        //console.log 
        alert('Error: ' + xhr.statusText + 'Status: ' + xhr.status); 
       }, 
       complete: function (xhr, status) { 
        //console.log 
        alert("The request is completed!"); 
       } 
      }); 

當我執行的代碼,我得到下面的錯誤控制檯

> XMLHttpRequest cannot load 
> http://127.0.0.1:54443/Service1.svc/getCurrentTime. Origin 
> http://127.0.0.1:57484 is not allowed by Access-Control-Allow-Origin. 

得到印刷作爲一種變通我沒有添加下面的設置來配置

(網站)文件
<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
<httpProtocol> 
     <customHeaders> 
     <add name="Access-Control-Allow-Origin" value="*" /> 
     </customHeaders> 
</httpProtocol> 
</system.webServer> 

仍然沒有得到任何有成效的結果。任何人都可以指出我失蹤的地方嗎?

注:我把它換成「本地主機」 127.0.0.1與感謝

回答