jQuery 呼び出し RESTful WCF サンプル コード (GET メソッド/POST メソッド)_jquery

WBOY
リリース: 2016-05-16 17:02:23
オリジナル
1166 人が閲覧しました

これ以上ナンセンスはやめて、本題に入りましょう

wcf 終了:

Restful は近年、Ajax 呼び出しを有効にし、Restful スタイルの URI をサポートするために、Ajax 対応の Wcf サービスを作成した後、手動で svc ファイルを変更し、Factory を指定する必要があります。

<%@ ServiceHost Language="C#" Debug="true" Service="ajaxSample.HelloWorld" CodeBehind="HelloWorld.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

注: Factory が追加されていない場合、http://localhost/helloWorld.svc/Hello/person/name などの RESTful メソッドを使用して wcf に直接アクセスできなくなります。

同時に、web.config の を削除します。これは、

と同様です。


🎜>



"
multipleSiteBindingsEnabled="true" /> binding="webHttpBinding" Contract="ajaxSample.HelloWorld" />



それでは、コードの作成を始めましょう。wcf を呼び出すときは GET/POST のメソッドが 2 つあるため、一般的に使用されるいくつかの状況に応じたサンプル メソッドを作成してみましょう。コード


コードは次のとおりです:


System.Collections.Generic を使用;
System.ServiceModel を使用します。
System.ServiceModel.Activation を使用;
System.ServiceModel.Web を使用;

namespace ajaxSample
{
[ServiceContract(Namespace = "http://yjmyzz.cnblogs.com/")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class HelloWorld
{

///
/// 只能Post的Restfulメソッド
///

///
///
///
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "PostRestfulTest/{person}/{welcome}", ResponseFormat = WebMessageFormat.Json)]
public List PostRestfulTest(string person,string welcome)
{
リスト result = 新しい List();

result.Add("PostRestfulTest -> サーバーから:");
result.Add(人);
result.Add(welcome);
結果を返します。
}

///
/// 只能取得的安静な方法
///
///
///
///
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "GETRestfulTest/{person}/{welcome}", ResponseFormat = WebMessageFormat.Json)]
public List GETRestfulTest(string person, string welcome)
{
List result = 新しい List();

result.Add("GETRestfulTest -> サーバーから:");
result.Add(人);
result.Add(welcome);
結果を返します。
}

///
/// 即時に与PostできるRestfulメソッド
///
///
///
///
[OperationContract]
[WebInvoke(Method = "*", UriTemplate = "RestfulTest/{person}/{welcome}", ResponseFormat = WebMessageFormat.Json)]
public List RestfulTest(string person, string welcome)
{
{
List result = new List();

result.Add("RestfulTest -> サーバーから:");
result.Add(person);
結果を返します; }


///
/// 只能Postの常规方法(注:Post方式、BodyStyle必須WrappedRequestまたはWrapped)
///

///
///
/// < returns>
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle=WebMessageBodyStyle.WrappedRequest)]
public List 、文字列 welcome)
{
List result = new List();

result.Add("PostRestfulTest -> サーバーから:");
result.Add(人);
result.Add(welcome);
結果を返します。
}

///
/// 只能取得的常规方法
///
///
///
///
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]
public List GETTest(string person, string welcome)
{
List result = 新しい List();

result.Add("GETTest -> サーバーから:");
result.Add(人);
result.Add(welcome);
結果を返します。
}




}
}

jQuery调用代码:

复制代码代码如下:












例代:

点击下ダウンロード
関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のおすすめ
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!