純粋な javasript/html ページで作業して、JSONP を使用して WCF Web サービスを呼び出し、クロスドメインの問題を回避し、「メソッドは許可されていません」というエラー メッセージを受け取り、オンラインで多くの検索を行いましたが、効果的な解決策を得ることができませんでした..
アーキテクチャ: WCF 3.5 + JSONP + JQuery
WCF 3.5 で JSONP 機能を有効にするために、MSDN JSONP サンプル ライブラリ ファイルを追加しました。
JSONPBehavior.cs
JSONPBindingElement.cs
JSONPBindingExtension.cs
JSONPEncoderFactory.cs
http://msdn.microsoft.com/en-us/library/cc716898.aspxから。この投稿に従って: http://jasonkelly.net/2009/05/using-jquery-jsonp-for-cross-domain-ajax-with-wcf-services/
IService1.cs:
namespace WcfServiceForConnDatabase
{
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "GetData")]
[JSONPBehavior(callback = "method")]
string GetData();
}
}
サービス.cs:
namespace WcfServiceForConnDatabase
{
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
public class Service1 : IService1
{
public string GetData()
{
return "Successfully connect with the WCF Web Service";
}
}
}
web.config:
<?xml version="1.0"?>
<configuration>
<!-- WCF configuration -->
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="WcfServiceForConnDatabase.Service1Behavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="WcfServiceForConnDatabase.Service1">
<endpoint address="" behaviorConfiguration="WcfServiceForConnDatabase.Service1Behavior" binding="customBinding" bindingConfiguration ="jsonpBinding" contract="WcfServiceForConnDatabase.IService1"/>
</service>
</services>
<bindings>
<customBinding>
<binding name="jsonpBinding" >
<jsonpMessageEncoding />
<httpTransport manualAddressing="true"/>
</binding>
</customBinding>
</bindings>
<extensions>
<bindingElementExtensions>
<add name="jsonpMessageEncoding" type="WcfServiceForConnDatabase.JsonpBindingExtension, WcfServiceForConnDatabase, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</bindingElementExtensions>
</extensions>
</system.serviceModel>
<system.web>
<compilation debug="true"/>
</system.web>
フロントエンド:
$.ajax({
url: "http://192.168.0.23/Service1.svc/GetData",
type: "GET",
jsonpCallback: "TestData",
contentType: "application/json",
dataType: "jsonp",
error: function () {
alert("Error");
},
success: function (data) {
alert("Success");
}
});
}
ネットワーク構造は次のようなものです。LAN 内の 2 台の PC。1 台はクライアント エンドの JavaScript ページ用、もう 1 台は Web サービス用 (192.168.0.23)。
誰かが私にいくつかの提案をしてくれることを願っています!!! ありがとうございます!