我編寫了一個返回JSON響應的WCF服務。然後我創建了一個帶有JavaScript代碼的HTML頁面來測試函數。當我將服務發佈到臨時環境(使用SSL模擬生產環境)時,我不得不更新服務的web.config文件以通過HTTPS進行工作。當我直接瀏覽到.svc端點(服務頁面同時顯示在http和https中)以及當我在瀏覽器中調用服務時(我被提示下載JSON響應)時,所有這一切似乎都可以,但是當我將測試頁更改爲指向https版本,我得到一個'訪問被拒絕'的錯誤。通過HTTPS從Javascript調用WCF服務導致拒絕訪問
這裏是我的配置文件的servicemodel部分代碼:
<system.serviceModel>
<services>
<service name="Services.PromotionCodeGeneration" behaviorConfiguration="md">
<endpoint address="" binding="webHttpBinding" contract="Services.IPromotionCodeGeneration" behaviorConfiguration="webHttp" bindingConfiguration="webBindingSecure"/>
<endpoint address="" binding="webHttpBinding" contract="Services.IPromotionCodeGeneration" behaviorConfiguration="webHttp" bindingConfiguration="webBinding"/>
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="md">
<serviceMetadata httpsGetEnabled="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webHttp">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="webBindingSecure">
<security mode="Transport"/>
</binding>
<binding name="webBinding">
<security mode="None"></security>
</binding>
</webHttpBinding>
</bindings>
</system.serviceModel>
這裏是我的測試頁的代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title></title>
<script>
function api_test() {
var url = "https://test.mydomain.com/Services/PromotionCodeGeneration.svc/contest/[email protected]";
var client = new XMLHttpRequest();
client.open("GET", url, false);
client.setRequestHeader("Authentication-Key", "LK589-JIJO-SHG9-0987-65TG-HJKU-Y$GH");
client.send();
var responseText = client.responseText;
var result = JSON.parse(responseText);
document.writeln("Id: " + result.Id + "<br/>");
document.writeln("Code: " + result.Code + "<br/>");
var expiryDate = "";
if (result.Expires != null){expiryDate = result.Expires;}
document.writeln("Expires: " + expiryDate + "<br/>");
document.writeln("Status: " + result.Status + "<br/>");
}
</script>
</head>
<body onload="api_test();">
</body>
</html>
我一直在研究這個問題2天。我發現很多人說你不能跨域使用'XMLHttpRequest'方法,但它可以爲我找到基本的http,所以我覺得很難相信。我也嘗試了許多不同的servicemodel配置建議,但是沒有一個用於https通信。有沒有人看到任何在我的配置或調用頁面導致通過https訪問「拒絕」的任何東西?
任何幫助,將不勝感激。
多個域的提及讓我覺得有沒有託管在同一個域?那是對的嗎?它不是你可能想要看看周圍的jsonp - http://stackoverflow.com/questions/2055186/json-and-xmlhttprequest – 2011-06-09 22:45:53