2013-11-27 39 views
0

我有一個「Hello World」 WCF服務:Jquery使用WCF。錯誤:XMLHttpRequest的無法加載地空沒有被允許訪問控制允許來源

public string GetData() 
{ 
    return "Hello world"; 
} 

<system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 
    <system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <serviceMetadata httpGetEnabled="true"/> 
      <serviceDebug includeExceptionDetailInFaults="true"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
     <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> 
    </system.serviceModel> 
<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    <directoryBrowse enabled="true"/> 
</system.webServer> 

而且我有一個非常簡單的HTML頁面:

$.ajax({ 
     type: "GET", 
     url: "http://localhost:49311/Service1.svc/GetData/", 
     dataType: "xml", 
     success: function() { 
      alert("test ok"); 
     }, 
     error: function (e) { 
      alert("error: " +e.responseText); 
     } 
    }); 

我收到以下錯誤:XMLHttpRequest無法加載localhost:49311/Service1.svc/GetData。原產地null是不允許的訪問控制 - 允許來源

是否有更簡單的方法解決這個跨站點/端口問題?

謝謝!

+0

您可以啓用現代瀏覽器 –

+0

感謝阿倫CORS的支持,但我認爲我需要在服務器端,我不能在每個客戶端上設置它。 – Kris

+0

在服務器端啓用cors,看到問題http://stackoverflow.com/questions/7234599/cors-support-within-wcf-rest-services –

回答

0

您的服務器端期待一些起源於從請求被髮送,目前它越來越空,用查爾斯或小提琴手來查看請求

+0

謝謝Varun!那麼這是否意味着我必須將一些原點發回給請求者?起源後,我應該怎麼做?謝謝!! – Kris

+0

如果你有Origin的話,它就會不知所措,因爲代碼抱怨你沒有Origin,你不必從頭文件返回任何東西 –

相關問題