目標:公開一個簡單的WCF服務,該服務將一個用戶名字符串作爲參數並查看它是否已經存在。此服務將用於我正在使用的網站的註冊頁面,因此當用戶輸入用戶名時,它會自動檢查是否已被使用。需要在JQuery/WCF上進行一些說明
我已經得到了這個功能,但我必須爲WCF服務啓用crossDomainScripting。我的問題是「爲什麼?」
給代碼!
我的WCF服務是我的MVC3站點內的託管(我本來在一個單獨的網站,但以消除任何和有關跨站點scriptiong所有關心我把它地方。)
接口WCF服務
[ServiceContract]
public interface IMembershipServices
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json,
UriTemplate = "IsUsernameAvailable/{username}",
BodyStyle = WebMessageBodyStyle.Bare)]
MembershipUserAvailability IsUsernameAvailable(string username);
[OperationContract]
[WebGet(UriTemplate = "helloto/{name}", ResponseFormat = WebMessageFormat.Json)]
string Hello(string name);
}
實現:
[AspNetCompatibilityRequirements(RequirementsMode
= AspNetCompatibilityRequirementsMode.Allowed)]
public class MembershipServices : IMembershipServices
{
#region IMembershipServices Members
public string Hello(string name)
{
return String.Format("Hello:{0}", name);
}
public MembershipUserAvailability IsUsernameAvailable(string username)
{
if (String.IsNullOrWhiteSpace(username))
{
throw new ArgumentException(username);
}
MembershipUser membershipUser = Membership.GetUser(username, false);
var membershipUserAvailability = new MembershipUserAvailability
{
UserName = username,
IsAvailable = membershipUser == null ? true : false
};
return membershipUserAvailability;
}
#endregion
}
的Web.config
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
<!-- This section is optional with the default configuration
model introduced in .NET Framework 4 -->
<service name="MedicalPracticeWeb.Services.MembershipServices" behaviorConfiguration="MembershipServicesBehaviors">
<!-- This endpoint is exposed at the base address provided by host: http://localhost/servicemodelsamples/service.svc -->
<endpoint address="" binding="webHttpBinding" bindingConfiguration="webHttpBindingJsonP" contract="MedicalPracticeWeb.Services.IMembershipServices" behaviorConfiguration="EndpBehavior" />
<!-- The mex endpoint is exposed at http://localhost/servicemodelsamples/service.svc/mex -->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MembershipServicesBehaviors">
<!-- Add the following element to your service behavior configuration. -->
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="EndpBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="webHttpBindingJsonP" crossDomainScriptAccessEnabled="true"></binding>
</webHttpBinding>
</bindings>
</system.serviceModel>
jQuery來imlement呼叫:
$(document).ready(function() {
var usernameTextbox = $('#username');
var usernameUnavailableRow = $('#usernameUnavailableRow');
var availabilityMessage = $('#availabilityMessage');
usernameTextbox.blur(function() {
if ($(this).val()) {
$.getJSON('/Services/MembershipServiceHost.svc/IsUsernameAvailable/' + escape($(this).val()), function (results) {
if (results.IsAvailable) {
if (usernameUnavailableRow.is(':visible')) {
availabilityMessage.html('This username is available.');
availabilityMessage.addClass('usernameAvailable');
availabilityMessage.removeClass('usernameTaken');
}
}
else {
usernameUnavailableRow.show();
availabilityMessage.html('This username is already taken!');
availabilityMessage.addClass('usernameTaken');
availabilityMessage.removeClass('usernameAvailable');
}
});
}
});
});
現在,這一切都很好。我可以在我的註冊表單上的文本框中鍵入用戶名,當我離開字段時,onBlur事件觸發並調用WCF服務,並返回結果,如預期的那樣...但只有如果我的web中有crossDomainScriptAccessEnabled =「true」。配置。這讓我感到困惑。也許這是對JSON和JSONP缺乏理解(完全可能,我對使用JSON相當陌生),但由於該服務託管在同一個域中,我爲什麼需要這個集合?
我看了一下什麼是小提琴手發送:
GET /Services/MembershipServiceHost.svc/IsUsernameAvailable/Tim?callback=jQuery1510988704698288691_1302393437642 & _ = 1302393485350 HTTP/1.1
而且有回調參數我在JQuery文檔中看到了這一點。
http://api.jquery.com/jQuery.getJSON/
如果URL中包含字符串 「回調=?」 (或類似的,由服務器端API定義),請求將被視爲JSONP。有關更多詳細信息,請參閱$ .ajax()中關於jsonp數據類型的討論。
這是否意味着成功回調的簡單行爲將強制GetJSON使用JSONP?
任何輸入讚賞。